C Experiment 3
C Experiment 3
Objectives –
Components Required –
1. Arduino Board
2. USB Cable
3. Ultrasonic Sensor
4. Jumper Wires
5. LCD Display
Circuit Diagram:
Figure. 1: Circuit diagram of distance measurement using Ultrasonic sensor and display
measured value in LCD.
Figure .2: An Ultrasonic sensor
Steps:
The distance measurement system using ultrasonic sensors with LED indication can be
implemented using the following steps:
Use Ultrasonic sensor with Arduino board.
Connect the VCC pin of the ultrasonic sensor to the 5V pin of the Arduino board.
The GND pin to the GND pin of the Arduino board.
The TRIG pin to pin 11 of the Arduino board, and the ECHO pin to pin 12.
Connect the LEDs to the Arduino board. Connect the anode (positive) pin of the red LED
to pin 4 of the Arduino board through a 220-ohm resistor.
Connect the anode (positive) pin of the green LEDs to pins 5, 6, and 7 of the Arduino
board, each through a 220-ohm resistor.
Upload the code and use the Arduino IDE to write and upload the code to the board.
System Test - Place an object in front of the sensors and observe the LED indication.
The red LED should light up if the object is within 10cm of the IR sensor, and the green
LEDs should light up to indicate the distance measured by the ultrasonic sensor.
Code:
14. nDevices = 0;
15. for (address = 1; address < 127; address++) {
16. Wire.beginTransmission(address);
17. error = Wire.endTransmission();
18. if (error == 0)
19. {
20. Serial.print("I2C device found at address 0x");
21. if (address < 16)
22. Serial.print("0");
23. Serial.print(address, HEX);
24. Serial.println(" !");
25. nDevices++;
26. }
27. else if (error == 4) {
28. Serial.print("Unknown error at address 0x");
29. if (address < 16)
30. Serial.print("0");
31. Serial.println(address, HEX);
32. }
33. }
34. if (nDevices == 0)
35. Serial.println("No I2C devices found");
36. else
37. Serial.println("done");
38. delay(5000); // wait 5 seconds for next scan
39. }
Additional Tasks –
Conclusions -