This approach is great for getting familiar with how the HC-SR04 module works. You should set the value delay(250) until the ultrasonic module can make the measurement. The delay setting can be lowered to receive more frequent measurements. To demonstrate this, I used my hand as an example and displayed the distance on the serial monitor. The serial monitor can show you the distance between two objects. The code then uses this to calculate the distance between the module and any obstacle by measuring the duration of the ultrasound wave. The pulseIn() function halts the program until it receives a response pulse from the HC-SR04 module on pin 18. The program generates a 10µs signal which is sent to the GP15 of the Pico. NewPing sonar (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE) // NewPing setup of pins and maximum distance.Const int trig_pin = 15 const int echo_pin = 14 // Vitesse du son dans l'air #define SOUND_SPEED 340 #define TRIG_PULSE_DURATION_US 10 long ultrason_duration float distance_cm void setup () Here’s an example code: # include # define TRIGGER_PIN 9 # define ECHO_PIN 10 # define MAX_DISTANCE 400 // Maximum distance we want to measure (in centimeters). Using the NewPing library we can get the distance with just a single line of code. Then according to those results we calculated the distance based on it. In the previously explained code we manually triggered the sensor and measured the received signal pulse duration. There are actually a simpler and better way to program the Arduino to measure distance using the HC-SR04 ultrasonic sensor, and that’s using the NewPing library. It offers excellent non-contact range detection with. void setup () Code language: Arduino ( arduino ) Example Code Using the NewPing Library The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. In the setup we have to define the trigPin as an output and the echoPin as an Input and also start the serial communication for showing the results on the serial monitor. Int distance Code language: Arduino ( arduino ) defines pins numbers const int trigPin = 9 Then we need a Long variable, named “duration” for the travel time that we will get from the sensor and an integer variable for the distance. In this case they are the pins number 9 and 10 on the Arduino Board and they are named trigPin and echoPin. Next, connect the middle pin (V OUT) to any of the analog inputs of the Arduino. Start by connecting the +V S pin to the 5 V output of the Arduino and the GND pin to the ground. Connecting an LM35 to the Arduino is very easy as you only need to connect 3 pins. } Code language: Arduino ( arduino ) Code Explanationįirst we have to define the Trig and Echo pins. Wiring Connecting LM35 analog temperature sensor to Arduino. Prints the distance on the Serial Monitor Serial. Reads the echoPin, returns the sound wave travel time in microseconds Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH) Clears the trigPin digitalWrite(trigPin, LOW) begin( 9600) // Starts the serial communication PinMode(trigPin, OUTPUT) // Sets the trigPin as an Output pinMode(echoPin, INPUT) // Sets the echoPin as an Input Serial. */ // defines pins numbers const int trigPin = 9 Ultrasonic Sensor HC-SR04 and Arduino Tutorial Here’s a code for measuring distance using the HC-SR04 ultrasonic sensor and Arduino. As an Amazon Associate I earn from qualifying purchases. Breadboard and Jump Wires ……… Amazon / Banggood / AliExpressĭisclosure: These are affiliate links.Arduino Board …………………………… Amazon / Banggood / AliExpress.Ultrasonic Sensor HC-SR04 ………… Amazon / Banggood / AliExpress.You can get these components need for this tutorial from any of the sites below: Arduino 06:01 Lora Transceiver connection on Breadboard 07LoRa SX1278/76 Arduino Interfacing Tutorial Sending Sensor Data Wirelessly with LoRa. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |