I have an ultrasonic sensor which i want to place upside. However when i do this, the sensor takes a reading automatically to the surface. I would like help to know how to code the sensor in such a way that when it is placed upside down it calibrates to zero and when it goes further down towards the surface the reading is shown from 0.
For eg:
Currently the sensor shows 104 mm, which is the distance to the table when placed upside down. However i want it to show 0 at this stage and when it goes further down it shows 6mm/7mm instead of 110mm/111m.
The first thing you need to do is figure out how you will detect when the sensor is "placed upside down". I can think of a few options:
accelerometer: The accelerometer detects the direction of gravity and so can be used to determine the orientation of an object.
tilt sensor: The tilt sensor closes the circuit when it is in an upright position and opens the circuit in other orientations.
use the HC-SR04 itself: If the ultrasonic sensor will get a significantly different reading between when it is "placed upside down" versus when it is in any other orientation, you can use that reading as the indication.
Once you have determined that the sensor is "placed upside down", you only need to take a distance reading and store that in a variable. Then use the value of that variable to zero all subsequent readings.
I am using Arduino Mega 2560 for Ultrasonic sensor to see the UART data but I am struggling with seeing the data.
I have connected power and ground of the sensor on Arduino board 3.3V and GND port and Tx Rx of the sensor is connected to Serial 1 Rx and Tx respectively. I have added a code(includes baud rate and request msg to get data from sensor) accordingly to see the data but I could not see any proper data on serial monitor consolle.
What you obviously also didn't do is read the "how to use this forum" sticky. Not only would you have known to start your own thread instead of hijacking, you would also have known what info to provide to get help.
Back to the OP:
aishdabaish:
Currently the sensor shows 104 mm, which is the distance to the table when placed upside down. However i want it to show 0 at this stage and when it goes further down it shows 6mm/7mm instead of 110mm/111m.
Just deduct that 104 mm from the distance you measured.