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

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

chart Information: The module includes ultrasonic transmitters, receiver, and control circuit in a single package.

Arduino Code:

// 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 ) ;

delayMicroseconds ( 5 ) ;

digitalWrite ( trigPin , HIGH ) ;

delayMicroseconds ( 10 ) ;

digitalWrite ( trigPin , LOW ) ;

pinMode ( echoPin , INPUT ) ;

duration = pulseIn ( echoPin , HIGH ) ;

cm = ( duration / 2 ) / 29.1 ;

inches = ( duration / 2 ) / 74 ;

Serial . print ( cm ) ;

Serial . println ( ) ;

delay ( 350 ) ; }

Download and Install Processing

Processing is available for Linux, Mac OS X, and Windows. Installto see The ruler on the screen

Processing Code:

//Example by Hisham Marzouk

//www.Officialhrm.com

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”);

size(1075,110);

background(bg);

String portName = Serial.list()[0];

myPort = new Serial(this, portName, 9600);

myPort.bufferUntil(‘.’);

try {

robot = new Robot();

}

catch (Exception e) {

e.printStackTrace();

}

}

void draw() {

//reading serial port until new Line

String sensorValue = myPort.readStringUntil(‘

’);

if (sensorValue != null) {

int value = Integer.parseInt(sensorValue.trim());

println(value);

background(bg);

ellipse(value *35.79, 55, 1, 115);

fill(33,77,88);

}

}

For more Please make sure to subscribe to Officialhrm YouTube channel and like Facebook Page.

Thank you for visiting Officialhrm! Please don’t forget to share this website with your friends.