官术网_书友最值得收藏!

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:

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...

  1. 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.

  2. Once you have finished uploading, open the Serial monitor. You should immediately see the readings from the sensor:
  3. 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.

主站蜘蛛池模板: 广南县| 大余县| 呼和浩特市| 绥江县| 中江县| 三河市| 岗巴县| 北京市| 泾源县| 剑川县| 阿拉善右旗| 滕州市| 东山县| 开化县| 新民市| 西城区| 隆回县| 井冈山市| 黄冈市| 基隆市| 巴东县| 彝良县| 张家界市| 花垣县| 德化县| 黄冈市| 贞丰县| 柳河县| 四平市| 怀宁县| 武安市| 杨浦区| 如皋市| 昭苏县| 渝中区| 石狮市| 高淳县| 霍州市| 德江县| 安丘市| 鄂尔多斯市|