Recently Cytron is carrying another new ultrasonic sensor which is called HC-SR04. Like other ultrasonic sensor, HC-SR04 uses sonar to determine distance to an object like bats or dolphins do. This sensor require a digital output (Trig) and input (Echo) pin to use it. We will show you the performance of this sensor by using Arduino. In order to make it easier, the HC-SR04 library in Arduino is also provided!
(i) Arduino IDE and Arduino Uno Driver – download from the Arduino download page
1. Prepare the hardware of the Arduino. Stack the Screw Terminal Shield and LCD Keypad Shield on the Arduino Uno.
2. Download the library and sample code of the ultrasonic sensor, SN-HC-SR04 under the attachment of this tutorial.
3. Open the sample code with Arduino IDE and upload it to the Arduino Uno. If you are a beginner to Arduino Uno, make sure you have already installed the driver on your computer. You can refer to the link below for getting started with the Arduino Uno.
4. After the sketch is uploaded, the LCD will display as below:
5. After making sure the Arduino is ready, it is time to interface with the ultrasonic sensor. Power off the arduino and connect the wire as shown below. Careful! Make sure the connection between Arduino and SN-HC-SR04 is correct. Wrong connection may spoil both hardware.
- VCC <–> 5V
- Trig <–> D12
- Echo<–> D13
- GND <–>GND
6. Power up the Arduino and now start testing the sensor by holding some object in front of the sensor. The LCD will show the distance between the sensor and the object in cm.