Introduction: Ultrasonic Sensor Test Plan for Chicken Coop Door (CL)
The goal of the following test plan on a ultrasonic or sonar sensor is to detect if the door of the chicken coop is closed. In this Intsructable you will learn step by step how to detect if a door is opened or closed. Below are instructions on how to properly wire the ultrasonic sensor to an Arduino UNO. Then you will learn how to program it, calibrate it, and test it out!
Step 1: Gathering Materials
In order to wire the sonar sensor you will need to lay out the materials listed below:
- Breadboard
- Ultrasonic Sensor
- USB cable
- Laptop
- wires
- Arduino Uno microcontroller
- Arduino
-LEDs
Step 2: Wiring the Sensor
After you have successfully gathered all the materials you might wonder what's next? In this step you will connect the sonar sensor to the Arduino Uno microcrontroller. The picture depicts the different ports of the sonar sensor. You should follow the diagram and connect the wires as such:
Connect wires from...
GND to GROUND
Echo to pin 10 (INPUT)
VCC to 5V
Trig to pin 11 (OUTPUT)
Step 3: Programming Arduino
Now that you have wired the sonar sensor to the Arduino Uno microcontroller, connect it to your laptop using the USB cable. Open Arduino and create a new file. This step will help you program, verify, and upload your code onto the microcontroller in order to start measuring distances! You can either copy the following code directly onto your file or play around with it. The pins in the code may vary from the ones you decide to use so keep that in mind because it might cause your code not to run.
int trig = 11; // Initializes variable trig and sets it to pin 11
int echo = 10; // Initializes variable echo and sets it to pin 10
float duration; //Creates float variable
void setup() { //Starts code
// put your setup code here, to run once:
pinMode(trig, OUTPUT); //makes variable trig an output
pinMode(echo, INPUT); // makes variable echo an input
Serial.begin(9600);
}
void loop() { // put your main code here, to run repeatedly:
digitalWrite(trig, LOW); //sets variable trig as low
delayMicroseconds(2); // Delays for 2 microseconds
digitalWrite(trig, HIGH); // Sets variable trig to high
delayMicroseconds(10); // Delays for 10 microseconds
duration = pulseIn (echo, HIGH); // this sets the “duration” as the time it takes the pulse to be
received.
delay(250); // Delays for 250 microseconds
Serial.println(duration); // Prints output for variable called duration
delay(250); // Delays for 250 microseconds
}
Step 4: Recording the Calibration Data
The next step after uploading your code is to test it out and see if it is functional. Open terminal from Arduino. You will notice that various numbers pop up. The closer you are changes how big the numbers are. These values are to be made into inches.
The function in the code "pulseln()" triggers a pulse which is then detected. It uses the speed of sound and will be converted to distance using time.
Insert this code into your main loop:
long duration, inches, cm;
Step 5: After Calibrating
We will now work with LEDs to show when the door is open or closed.
Obtain two different color LEDs. One of these will be set to light up when the door is opened and the other will light when the door is fully closed.
Image source: https://www.instructables.com/id/How-to-make-A-light-up-distance-sensor/
Comments
5 years ago
That's a neat idea, I'd love to see it once the project is done :)