![]() - // // Arduino Ultrasoninc Sensor HC-SR04 // Re-writed by Arbi Abdul Jabbaar // Using Arduino IDE 1.8. We need to apply a short 10 s pulse to the trigger input to start. To display the measurement data you can use Serial Monitor by pressing Ctrl + Shift + M (make sure that the baudrate speed is 9600) The timing diagram of waveform on each pin is shown in the following diagram. Upload your code by pressing Ctrl + U or Sketch > UploadĦ. (If there are more than one ports, try it one by one)ĥ. ![]() Choose your COM Port (usually it appears only one existing port), Tools > Port > COM. Choose your own Arduino board (in this case Arduino Uno), by selecting Tools > Board > Arduino/Geniuno UnoĤ. ![]() Open Arduino IDE Software and write down your code, or download the code below and open itģ. First do the wiring as shown in the pictureĢ. At the end we will print the value of the distance on the Serial Monitor.ġ. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation previously. At the end the function will return the length of the pulse in microseconds. In this case, HIGH means that the pulseIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. This function has 2 parameters, the first one is the name of the echo pin and for the second one you can write either HIGH or LOW. Using the pulseIn() function you have to read the travel time and put that value into the variable “duration”. Now for generating the ultrasound wave we have to set the trigPin on HIGH State for 10 µs. In the loop first you have to make sure that the trigPin is clear so we have to set that pin on a LOW State for just 2 µs. Then define variables for the distance (int) and duration (long). The Echo is a distance object that is pulse width and the range in. To start measurement, Trig of SR04 must receive a pulse of high (5V) for at least 10us, this will initiate the sensor will transmit out 8 cycle of ultrasonic burst at 40kHz and wait for the reflected ultrasonic burst. In this project EchoPin is attached to D2 and TrigPin to D3. You only need to supply a short 10uS pulse to the trigger input to start the ranging, and then the module will send out an 8 cycle burst of ultrasound at 40 kHz and raise its echo. Unsigned long firstSampleTimer, secondSampleTimer ĭistance = ( ( ((float)(secondSampleTimer - firstSampleTimer)) * SOUND_SPEED ) / 20000.For the programming code, first we need to define the Trigger Pin and Echo Pin that connected to Arduino board. The software for the Arduino Uno board that implements this algorithm, see below: Measure the duration of the response echo-pulse.Change the mode of I/O port P0 to input mode.Keep the P0 port in low level state during 10 µs.Switch the control I/O port P0 of the microcontroller to output mode.In order to use the same input/output port both for issuing a short start pulse and for measuring the duration of the response echo-pulse, the microcontroller must control this I/O port in the following order, as described below: So, the inverted copy of this echo-pulse is applied to the P0 control port (see yellow waveform below), that works as input (in input mode), while the VT2 switch blocks the Trig input from a false start pulse at the same time. The response high level pulse generated by the HC-SR04 ultrasonic ranging module at its Echo output (see violet waveform below) closes the VT2 and VT3 switches for the duration of this pulse. At the same time, the high level at the gate of the transistor VT1 will be maintained by the resistor R1, respectively, the Trig input of the HC-SR04 ultrasonic ranging module will be in low level state. Immediately after this, the microcontroller must change the mode of operation of its PB0 control port from output mode to input mode. So, the VT1 switch opens and switches the Trig input of the HC-SR04 ultrasonic ranging module to high level state for about 10 µs (see turquoise waveform below). The microcontroller puts the PB0 port in low state to apply a short pulse with a duration of about 10 µs at the gate of the VT1 MOSFET-switch (see yellow waveform below). The state of the Echo output of the HC-SR04 module and the gates of transistors VT2 and VT3 is still low and, accordingly, the VT2 and VT3 switches are kept opened, so that they do not have any effect on the state of the control signals. Before the microcontroller issues a short start pulse, its port PB0 (pin 14) is in the output mode and its state is high level (see yellow waveform below), so the VT1 switch is closed and the state of Trig input of the HC-SR04 module is low.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |