Menu Close
Spread the love

HC-SR04 Arduino Nano

Arduino HC-SR04 Ultrasonic Sensor and Processing

Parts Required:

–  Arduino NANO v3.0
–  HC-SR04 Ultrasonic Sensor
–  Wires to connect it all together

HC-SR04 Arduino Nano

Connect the components and wires as shown in the two pictures.




The module includes ultrasonic transmitters, receivers, and a control circuit in a single package.

Arduino Code:
Arduino Menu up
// Example by Hisham Marzouk
int trigPin = 2;//Trig – green Jumper
int echoPin = 3;    //Echo – yellow Jumper
long duration, cm, inches;
void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
void loop() {
 digitalWrite(trigPin, LOW);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 cm = (duration/2) / 29.1;
 inches = (duration/2) / 74;
 delay(350); }
Arduino Menu Down
Download and Install  Processing to see The ruler on the screen
Processing is available for Linux, Mac OS X, and Windows.

Processing Code:

Processing Menu Up

//Example by Hisham Marzouk

import processing.serial.*;
import java.awt.event.KeyListener;
import java.awt.Robot;
import guru.ttslib.*;

Serial myPort;
Robot robot;
TTS tts;
String val;
boolean valtrue;
int xy = 0;
int x=0;
PImage bg;
void setup()
bg = loadImage(“lside.jpg”);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
try {
robot = new Robot();
catch (Exception e) {
void draw() {
//reading serial port until new Line
String sensorValue = myPort.readStringUntil(‘\n’);
if (sensorValue != null) {
int value = Integer.parseInt(sensorValue.trim());
ellipse(value *35.79, 55, 1, 115);

Processing Menu Down
Thank you for visiting. If you found this information helpful, please consider subscribing to the Officialhrm YouTube channel and liking their Facebook Page for more updates and helpful content. Don’t forget to share this website with your friends.


File Description Date added File size Downloads
zip Arduino HC-SR04 diagram August 15, 2018 4:15 pm 505 KB 10314
Posted in Arduino

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *