We can Blink a 1.8V Bulb with our previous Arduino Project. Ultrasonic Sensor HC-SR04 is a Cheap Proximity Sensor For Robotics Projects. We can Light a Bulb With Arduino Ultrasonic Sensor Easily by Modification of the Previous Project. This sensor can measure a distance of 2 cm to 4 meters. This is very commonly used sensor for wheeled Robot to avoid the obstacles. Usability of this Arduino Ultrasonic Sensor HC-SR04 is higher and we can use for any other projects.
Light a Bulb With Arduino Ultrasonic Sensor HC-SR04 : Buying Guide
Perform a Web Search with the phrase Ultrasonic Sensor HC-SR04 and you will get various Adverts from Ebay, Amazon etc. It should not cost more than $1.5. There is no special precaution or guide needed for this stuff. Other needed materials has been in our previous Arduino Project. However, one can Blink only a LED to avoid the need of the other things. In such case, only an Arduino board, the Ultrasonic Sensor HC-SR04 module and some jumpers becoming the need.
Light a Bulb With Arduino Ultrasonic Sensor HC-SR04 : Circuit and Coding Part
We will code in a way, so that we can improve the project in future. In the Ultrasonic Sensor HC-SR04 module, there are 4 pins :
---
VCC is applied voltage from 4.5 V to 5.5 V
Trig is the Sensor input – trigger.
Echo is the Output Sensor.
GND is Ground or -ve pole.
Circuit is easy, it is modification of the previous bulb blink :
Attach the Ultrasonic Sensor to the breadboard
Connect the GND pins to Bulb’s one pole, one red LED’s -ve and the sensor (GND).
Attach the Bulb to Pin 3 on the Arduino
Attach the Red LED to Pin 2 on the Arduino
Attach the VCC pin on the Ultrasonic Sensor to 5V
Attach the trig pin on the Ultrasonic Sensor to Pin 13 on the Arduino
Attach the echo pin on the Ultrasonic Sensor to Pin 12 on the Arduino
Copy the code written below. There is nothing to see as circuit diagram. We have no Green colored LED but bulb with a different power source. For compatibility reasons, we used bulbLed
to indicate Bulb. One can replace the Bulb with a Green LED. This is the approximate circuit :
Desired result :
When something is 5 cm away from the Ultrasonic Sensor HC-SR04 module, the bulb will light, else the red LED will light up.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | const int pingPin = 13; int inPin = 12; int bulbZone = 5; int bulbLed = 3, redLed = 2; void setup() { Serial.begin(9600); } void loop() { long duration, cm; pinMode(pingPin, OUTPUT); pinMode(bulbLed, OUTPUT); pinMode(redLed, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW); pinMode(inPin, INPUT); duration = pulseIn(inPin, HIGH); cm = msCms(duration); Serial.print(cm); Serial.print("cm"); Serial.println(); if (cm > bulbZone) { digitalWrite(bulbLed, HIGH); digitalWrite(redLed, LOW); } else { digitalWrite(redLed, HIGH); digitalWrite(bulbLed, LOW); } delay(100); } long msCms(long microseconds) { return microseconds / 29 / 2; } |
Here is the gist on GitHub of the above code.
Tagged With https://thecustomizewindows com/2015/11/light-a-bulb-with-arduino-ultrasonic-sensor-hc-sr04/ , arduino ultrasonic sensor light code , sensor ultrasonico com led arduino , led reactive ultrasonic , arduino hc-sr04 и светодиоды , arduino sr04 with led blinking , arduino ultrasonic code and light , Ультразвуковой датчик на ардуино проекты , arduino ultrasonic lights , arduino ultrasonic sensor hc-sr04