- Internet of Things with Arduino Cookbook
- Marco Schwartz
- 449字
- 2021-07-08 11:26:53
Interacting with basic sensors
In this recipe, we are going to see how to measure data coming from sensors connected to the MKR1000 board. This will really teach us the very basics of the Arduino language. As an example, we'll use a simple photocell to measure the ambient light level around the project.
Getting ready
For this project, you will need a few extra components in addition to the Arduino MKR1000 board and the usual breadboard and jumper wires:
- Photocell (https://www.sparkfun.com/products/9088)
- 10K Ohm resistor (https://www.sparkfun.com/products/8374)
We are now going to assemble the project. First, place the resistor in series with the photocell on the breadboard, next to the MKR1000 board.
Now, connect the other end of the resistor to GND on the MKR1000 board, and the other end of the photocell to the VCC pin of the Arduino board. Finally, connect the middle pin between the resistor and the photocell to analog pin A0 of the MKR1000.
This is the final result:

How to do it...
- We are now going to configure the board to read data coming from the photocell. The sketch for this part will be really simple, as we will simply print the readings of analog pin A0 on the serial port. This is the complete sketch for this part:
// Pins int sensorPin = A0; void setup() { // Serial Serial.begin(115200); } void loop() { // Reading int sensorValue = analogRead(sensorPin); // Display Serial.print("Sensor reading: "); Serial.println(sensorValue); // Wait delay(500); }
Tip
You can now simply copy this sketch and paste it inside your Arduino IDE. Make sure that you connected the board to your computer via USB, and select the right board and Serial port inside the Arduino IDE. Then, upload the sketch to the board.
- Once you have finished uploading, open the Serial monitor. You should immediately see the readings from the sensor:
- Now, simply try to put your hand on top of the sensor. You should immediately see the value measured by the sensor coming down, meaning the sensor is working correctly.
How it works...
This project was really simple, and illustrated how to read data from an analog pin on the MKR1000 board. In this project, we simply read data on analog pin A0, and printed the readings on the Serial monitor. As the photocell is acting as a variable resistor (depending on the ambient light level), we are directly reading a signal that changes depending on the ambient light level.
See also
You can now move on to the next recipe that will show you how to control outputs on the board, or even to the recipes. After that, you will learn how to send measurement data on the cloud.
- ExtGWT Rich Internet Application Cookbook
- Intel Galileo Essentials
- 自己動手寫搜索引擎
- R語言數據分析從入門到精通
- 編程的修煉
- React Native Cookbook
- Raspberry Pi 2 Server Essentials
- Python數據可視化之Matplotlib與Pyecharts實戰
- RESTful Java Web Services(Second Edition)
- Django 3.0入門與實踐
- 計算機應用基礎教程(Windows 7+Office 2010)
- 監控的藝術:云原生時代的監控框架
- JavaScript悟道
- AI自動化測試:技術原理、平臺搭建與工程實踐
- INSTANT Premium Drupal Themes