Hello guys, today I am here to explain how to connect your Arduino board or your NodeMcu board with the Ultrasonic Sensor and the LDR Sensor. I will also be explaining how to use a LED and the Serial.Monitor for user feedback. I will try to keep this guide as simple and clear as humanly possible but in case of questions don't hesitate to ask, I can't stress this enough. Now that we have the boring introduction stuff behind us, how about we get some work done, shall we?
What will we need for this project?
First of all, here is a short list with stuff that we will need for this project, they are extremely cheap and very easy to get your hands on.
- 1 Micro USB cable
- 1 10k ohm Resistor
- 1 Ultrasonic Sensor
- 1 LDR Sensor / Photoresistor
- 1 Arduino or NodeMcu Board (I will be using the NodeMcu.)
- Multiple female-to-female jumper wires. (The ones that have holes on both sides. NOT the ones with the pin.)
- 1 LED (Any color will do, I will be using a red colored one for this example.)
Step 1: How to connect the LDR Sensor to your board & the required code.
You cannot connect your LDR sensor direct to the board! Please make use of a 10k ohm resistor otherwise this will not work and you may damage your sensor. Use the diagram below as your guide.
const int ldr = A0;
int ldrVal = 0;
void setup() {
}
void loop() {
ldrVal = analogRead(ldr); // The ldrVal Variable will now contain the data gathered by the sensor.
delay(2000); // This Variable will be updated every 2 seconds.
}
Step 2: How to connect the Ultrasonic Sensor to your board & the required code.
const int trigPin = D1;
const int echoPin = D2;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // The distance Variable will now contain the data gathered by the sensor. In this case the distance in CM.
delay(2000); // This Variable will be updated every 2 seconds.
}
Step 3: How to use your LED and the Serial.Monitor for user feedback:
At the moment we have two Variables containing our gathered Sensor Data. The LDR data is being saved in a Variable named ldrVal while the Ultrasonic Sensor data is being saved in a Variable named distance.
Let's see if we can display this information somewhere. First of all, we will need to open the Serial.Monitor. You will be able to find it under Tools > Serial Monitor. Make sure your board is connected and the value from the drop-down is set to 9600.
After you successfully completed the previous tasks, add the following lines of code to your program:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(ldrVal); // Prints the ldrVal content to the Serial.Monitor.
Serial.println(distance); // Prints the distance content to the Serial.Monitor.
}
Great! You can now gather information from sensor data and display it to your users. Let's enhance this experience with light feedback. Use the diagram below to connect your LED to the board.
Don't forget to update your code:
void setup() {
pinMode(D3, OUTPUT);
}
To turn the LED light on, for example when the value of distance is greater than 35 centimeters use the code below:
void loop() {
if (distance > 35)
{
digitalWrite(D3, HIGH);
}
}
And to turn it back off:
void loop() {
if (distance < 35)
{
digitalWrite(D3, LOW);
}
}
A note of warning, if you are using a red LED like me, don't let it burn for longer then 2 seconds, your LED will burn! You can however instead of turning the LED continues on, make it blink every 0.5 seconds or more, this will prevent it from burning:
void loop() {
if (distance > 35)
{
digitalWrite(D3, HIGH);
delay(500);
digitalWrite(D3, LOW);
delay(500);
}
}
I hope that you guys found this simple guide somewhat useful. Like I said in the introduction, in case of questions or in case you run in any kind of error please don't hesitate to message me.
Comments