5059+ reviews
Order by 16:00 for same day shipping
14 days return
GB
EN
Individual
Business
This is the fourth lesson in the Arduino IoT Cloud manual. Haven't followed the first lessons yet? Check out these HERE . In this lesson you will learn how to read the value of an HC-SR04 ultrasonic distance sensor in the Arduino IoT. We store these values so that we can see them in a graph.
For the project we create a new “Thing”. You can also use one of the “Things” from the previous lessons. But to keep everything clear we create a new “Thing” for this project. In this project we named the “Thing” Arduino-IoT-lesson-4-Distance-Sensor. Once the thing is created we add a property to it. In this case the property is a Distance sensor. For type we go for “Length (centimeters)” again. We only want to read data so the permission must be set to read only. At the bottom of the page you see History. We select this to save the incoming data. This allows us to later view a graph with the measured values.
Now that we have created the property we will build and wire it.
In the image below you can already see some data. You will only see this once the code has been uploaded. The current distance will be shown in the left image. At the top right you will see a small graph. If you click on this you will see the right image. In this, the collected data is converted into a graph. You can see the last minute, 30 minutes, hour, etc.
Now we're going to put the project together.
You start by placing the Arduino Nano 33 IoT on the breadboard. In the middle of the breadboard is a slot. Make sure that the pins of the board are on both sides of the slot as shown below. The HC-SR04 needs a 5V power supply. To supply the sensor with a constant voltage we use a Breadboard power supply. The HC-SR04 has 4 pins. The VCC is connected to the + track of the breadboard because there is now 5V on it. Then we connect the Trig (trigger) pin to D11. We connect the Echo pin to D12 and the GND pin to the - track of the breadboard.
Attention! If you use the Breadboard power supply make sure the jumper cap is set to 5V instead of 3.3V.
Now that you have finished building and wiring the circuit, you can start programming it.
We program the Arduino in the same way as in the previous lessons. The code you will use for this is below. If you want to learn it better, type the code instead of copying and pasting it. You will learn to program better that way.
Once you have written the code you can upload it to the Arduino. If this is successful you go back to the IoT Cloud, you click on dashboard. If all goes well, data is now displayed as in the images in step 1.
#define ULTRASONIC_TRIG_PIN 11 // pin TRIG is D11
#define ULTRASONIC_ECHO_PIN 12 // pin ECHO is D12
#include “thingProperties.h”
void setup() {
Serial.begin(9600);
delay(1500);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
pinMode(ULTRASONIC_TRIG_PIN, OUTPUT); //De Trigger wordt gedefinieerd als output
pinMode(ULTRASONIC_ECHO_PIN, INPUT); //De Echo wordt gedefinieerd als input
}
void loop() {
ArduinoCloud.update();
long duration, Afstand_Sensor;
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(ULTRASONIC_TRIG_PIN, HIGH); //Hiermee zenden we een ultrasoon geluid
delayMicroseconds(10); //Het zenden doen we 10 miliseconden
digitalWrite(ULTRASONIC_TRIG_PIN, LOW); //Nu stoppen we het zenden
duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH); //Hier meten we hoelang het duurt voordat we het ultrasone geluid terug krijgen.
afstand_Sensor = (duration/2) / 29.1; //Hiermee zorgen we dat de waardes in cm worden weergegeven in de IoT cloud
Serial.print(” Afstand: “); //Om zonder IoT Cloud verbinding te kunnen controleren of alles werkt weergeven we het ook in de Monitor
Serial.print(afstand_Sensor);
Serial.println(” cm”);
}