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.