Introduction
We will measure the distance between the cupboard/fridge door and Ultrasonic sensor constantly and update it on the web. Whenever the cupboard door is opened, the distance would show a spike.
Calculating Distance Using Ultrasonic Waves
An Ultrasonic sensor measures distance using speed of sound. Basically, the transmitter sends an Ultrasonic sound wave towards an object and the receiver part receives the sound wave which is reflected from the same object. We know that the speed of sound is 330 meters per second. The sensor gives us the time taken by the wave to hit the object and getting back to the receiver. Since we know the time and speed, we can easily calculate the distance of the object from the sensor.
Fig. 1: Image representing working of Ultrasonic Sensor
Let us say that the object is at a distance=‘d’ meters from the sensor. The time taken by the ultrasonic wave (shown in red) to hit the object and reflect back be T seconds and speed of the wave = ‘S’ m/s. Then,
2 X d = S X T
=> d = S X T / 2
We take twice the distance because the wave travels from Tx to object and then object to Rx. And since we know that S=330m/s we get,
d = 165 X T meters (Where T is in seconds)
So all we need to know is the time taken by the wave to travel to and fro. This time duration is provided by the ‘Echo’ pin of the SR-04 module.
ESP8266 In Client Mode
Thingspeak.com is a website where you can upload certain data to a ‘channel’ you created and can also see the same data in form of a graph and that too with real-time update. When you create an account and a channel, you get a code which is also known as a ‘Key’ which would be used in order to upload date into the particular channel.
Procedure:
1. Go to www.thingspeak.com and proceed to sign up for a free account.
Fig. 2: Screenshot of API Key Generation on Thingspeak Server
4. Now, navigate to ‘API’ tab and note down the “API key”
5. To test the channel you just created, type in the following on the address bar in your browser:
https://api.thingspeak.com/update?key=<API_KEY>&field1=0
Substitute <API_KEY> with the one you got in 4th step.
And then press enter. You should see a blank page with just ‘1’ written on it.
6. Now come back to the channel page and navigate to the ‘Private view’ tab and you should see something like this:
Fig. 3: Screenshot of Channel Statistics displayed on Thingspeak Server
Code Explanation
Arrangement
Fig. 4: Prototype of Arduino and ESP8266 Wi-Fi Modem based Circuit used for Data Logging
Fig. 5: Image showing Ultrasonic Sensor Interfaced to Arduino Uno
Here are some outputs I got:
Fig. 6: Screenshot of Cupboard Statistics displayed on Thingspeak Server
As you can see, when I opened and took time to setup the system there properly, it already started sending data since there was a difference in distance of the cupboard’s door.
After that, at around 12:00, it stopped uploading data since the cupboard was closed and hence there was no change in distance between the sensor and the door for a while. And then at around 12:52 when I open the door, it senses the change of distance and again starts uploading data.
Troubleshooting
You may also like:
Project Source Code
int pingPin = 16; // A2 - Connected to both Trig & ECHO Pins of the Sensorunsigned int cm_old=0;void setup(){Serial.begin(9600);Serial.println("AT");delay(5000);if(Serial.find("OK")){connectWiFi();}}void loop(){long duration,cm;// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:pinMode(pingPin, OUTPUT);digitalWrite(pingPin, LOW);delayMicroseconds(2);digitalWrite(pingPin, HIGH); // Sending a High Pulse on the Trig Pin of the SensordelayMicroseconds(5);digitalWrite(pingPin, LOW);// The same pin is used to read the signal from the PING))): a HIGH// pulse whose duration is the time (in microseconds) from the sending// of the ping to the reception of its echo off of an object.pinMode(pingPin, INPUT);duration = pulseIn(pingPin, HIGH);// convert the time into a distancecm = microsecondsToCentimeters(duration);String cmstr = String(cm);if(cm!=cm_old) updateDist(cmstr);delay(2000);cm_old=cm;}void updateDist(String cmstr){String cmd = "AT+CIPSTART="TCP","";cmd += "184.106.153.149";cmd += "",80";Serial.println(cmd);delay(2000);if(Serial.find("Error")){return;}cmd = "GET /update?key=&field1="; //edit the key and copy paste with your own one cmd += cmstr;cmd += "rn";Serial.print("AT+CIPSEND=");Serial.println(cmd.length());if(Serial.find(">")){Serial.print(cmd);}else{Serial.println("AT+CIPCLOSE");}}boolean connectWiFi(){Serial.println("AT+CWMODE=1");delay(2000);String cmd="AT+CWJAP="";cmd+=""; cmd+="","";cmd+=""; cmd+=""";Serial.println(cmd);delay(5000);if(Serial.find("OK")){return true;}else{return false;}}long microsecondsToCentimeters(long microseconds){// The speed of sound is 340 m/s or 29 microseconds per centimeter.// The ping travels out and back, so to find the distance of the// object we take half of the distance travelled.return microseconds / 29 / 2;}
Circuit Diagrams
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.