High Voltage Engineering- OVER VOLTAGES IN ELECTRICAL POWER SYSTEMS
Arduino tutorial #4
1. 1
Arduino Tutorial #4
- Measuring the distance -
Dong Ho Son
POSTECH
Pohang, Korea
donghoson@postech.ac.kr
2. 2
Measuring the distance - Overview
Infrared proximity sensor (GP2Y0A21YK0F)
• A sensor to detect the presence of nearby objects without any physical contact
• The sensor has an analog output that varies from 3.1V at 10cm to 0.4V at 80cm
How the circuit works
• Infrared proximity sensor will sense the distance and send the result to the
Arduino board
• A serial monitor shows the distance from the sensor to the target object
Source: commons.wikimedia.org/wiki/File:Sharp_GP2Y0A21YK_IR_proximity_sensor.jpg
3. 3
Measuring the distance – configuring the circuit
Configure the circuit
VOC
GND
Vout
Source: pixabay.com/photo-2168193
4. 4
Measuring the distance - Sketch
Declare variables
• float Vcc = 5.0; Voltage
• float dist; Distance
setup() { }
• Serial.begin(9600);
− Prepare the serial communication to get
the value of distance (from the sensor)
− 9600 : serial communication speed
– 9600 bits per second is the default for the Arduino
5. 5
Measuring the distance - Sketch
loop() { }
• dist = Vcc * analogRead(A0) / 1023;
− Read analog value of infrared distance
− analogRead(A0) returns the value as 10 bit
− 210=1024, 0~1023 values are returned
− Dividing the output by 1023 the distance
• dist = 26.549 * pow(dist, -1.2091);
− Formula [Distance = 26.549 × 𝑉𝑜𝑙𝑡𝑎𝑔𝑒−1.2091]
http://image.dfrobot.com/image/data/SEN0014/gp2y0a21yk0f.pdf
6. 6
Measuring the distance - Sketch
loop() { }
• Serial.print(“ Dist = ”);
− It prints the text “ Dist = ” to the serial console
• Serial.print(dist);
− It prints the value of ‘dist’ to the serial console
• Serial.println(“ cm ”);
− It prints the text “ cm ” to the serial console
− Also changes the line (newline)
• Delay(300);
− Need 300ms to check step-by-step
7. 7
Measuring the distance - Result
The sensor measures the distance!
10cm
Dist = 10.00 cm
20cm
Dist = 20.00 cm
Dist = 40.00 cm
40cm
Dist = 80.00 cm
80cm
VOC
GND
Vout
Source: pixabay.com/photo-2168193