Posted on Leave a comment

HC-SR04 Connection to Arduino Example

HC-SR04 Ultrasonic Front

This example shows HC-SR04 connection to Arduino. This example is basic setup and can be integrated into any other project and get the distance as final from either the cm or inch variables.

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

This example uses Arduino Uno, still, it can work for any Arduino model.

HC-SR04 Connection Steps

First implement the HC-SR04 connection to Arduino Uno as depicted in the image below:

HC-SR04 connection to Arduino UNO

Then use the following code with the Arduino:

// HC-SR04 Connection to Arduino Uno Example from TechDepot Egypt

const int trigPin = 7;
const int echoPin = 8;
long reply, cm, inch;

void setup() {

pinMode(trigPin, OUTPUT); // Prepare the trigger pin
pinMode(echoPin, INPUT);  // Prepare the echo pin



void loop() {

// Reset the trigger pin and get ready for a clean trigger pulse

digitalWrite(trigPin, LOW);

// Generate and send to trigger pin the trigger pulse

digitalWrite(trigPin, HIGH); // You need to keep it high for 10 micro seconds length
delayMicroseconds(10);       // This is the 10 microseconds we mentioned above :)
digitalWrite(trigPin, LOW);  // Stop the trigger pulse after the 10 microseconds

// Now let's see how long did it take the sound wave to travel
// It will take a time depending on the distance to the obstacle
// This time may be up to 38 millisecond in case of no obstacle
// If you never saw pulseIn before please check:

reply = pulseIn(echoPin, HIGH);

// Now we have the reply in microseconds, but we need a distance!


// Let's print the distance to the Arduino's Serial Monitor. Tools --> Serial Monitor

Serial.print("Distance in Inches ");
Serial.print(" - Distance in cm ");


void time2Distance(long rawReply) {

// The data sheet says that it takes sound 73.746 microseconds to travel 1 inch.
// So if we divide rawReply which is in microseconds by the above we will get how many
// inches the sound travelled forward and travelled back to the sensor.
// The rawReply is the sound travelling to obstacle and back, then we need to divide by 2

inch = rawReply/73.746/2;

// According to Google, 1 inch eaquales 2.54 cm, accordingly and to get distance in cm

cm = inch*2.54;


You are all done. Enjoy 🙂

Posted on Leave a comment

HC-05 Connection to Arduino

HC-05 Bluetooth Module Main

The HC-05 Bluetooth module is an excellent interface for communicating with your mobile. It has a great data transfer rate and very easy to implement.

In order to try this tutorial out you will need the following:

  • Any Arduino board you like and if you don’t already have one check these out
  • Grab a breadboard similar to any one of these
  • Get yourself one of these HC-05 Bluetooth Modules, if you don’t already have one then grab one from here
  • 2 Resistors, 2K Ohm and 4.6K Ohm
  • Finally grab some Dupont jumper wires or use normal wires if you are comfortable with that

It is worth noting that the HC-05 power in (Vcc) uses 5V, while the transmit and receive (TXD and RXD) logic signal uses 3.3V. Accordingly sending signals from the HC-05 module to Arduino is ok as the Arduino   I/O pins can safely receive up to 5V but the issue is when Arduino tries to send the data to the HC-05 with signal level 5V, in this case it is required to use a voltage divider as we will see during the tutorial.

HC-05 Bluetooth module connection

Now let’s get down to business. First and in order to avoid a great deal of frustration and time wast, if your breadboard has a power rail attached test it for connectivity.

1- Get 2 dupont jumper cables, connect 1 at the rail start and one at the rail end. Now grab your voltmeter and set it on Ohm or connectivity measuring mode then connect the tips to the end of the jumper cables. Make sure that the rail is consistent.

Breadboard power rail connectivity

2- If the power rail connectivity is not consistent then consider using jumper wires to fix that.

Breadboard power rail jumper

3- Connect the Arduino Nano and connect it 5V out and GND to power rails as depicted in the image below.

Arduino Nano connected

4- Now let’s connect the HC-05 and its power sockets. Vcc to the Arduino 5V and GND to Arduino GND.

HC-05 Connection step1

5- Next we will connect the HC-05 TXD (this is the pin where all data received from the mobile will be transmitted via it to Arduino) to Arduino pin D3.

Step2 - HC-05 TXD connection

6- Now we will have to connect the HC-05 RXD. First we need to establish a voltage divider using the 2K and the 4.6K ohm resistors. First connect one end of the 2K resistor to Arduino D2 pin, the connect the other end to any unused line in the breadboard, then into this line connect the 4.6K resistor and the other end to the GND. See the image below.

HC-05 voltage divider for RXD connection

7- Connect a dupont jumper originating from the centre point between the 2 resistor and connect it to the HC-05 RXD.

HC-05 RXD connection

8- You are all done now with the hardware work. Let’s move to programming. Grab you USB cable and connect the Arduino to your laptop and use the code below:

#include <SoftwareSerial.h>

// Define the data transmit/receive pins in Arduino
#define TxD 2
#define RxD 3

SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth

void setup() {

  mySerial.begin(9600); // For Bluetooth
  Serial.begin(9600);   // For the IDE monitor Tools -> Serial Monitor

  // Any code that you want to run once....


void loop() {

  // put your main code here, to run repeatedly:

  boolean isValidInput;
    byte c; // get the next character from the bluetooth serial port
    while ( !mySerial.available() )
      ; // LOOP...
    c =; // Execute the option based on the character recieved

    Serial.print(c); // Print the character received to the IDE serial monitor

    switch ( c )
      case 'a': // You've entered a

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'a'" );
        isValidInput = true;

      case 'b': // You've entered b

        // Do the code you need when 'a' is received.....

        mySerial.println( "You've entered an 'b'" );
        isValidInput = true;


        // Do the code you need when any other character is received.....

        mySerial.println( "Please enter 'a' or 'b'" );
        isValidInput = false;
  } while ( isValidInput == true ); // Repeat the loop

Oh, forgot to tell you, download and use BT-Term (S2 Terminal for Bluetooth) downloadable from the Google PlayStore to send characters to your module 🙂

Hope this tutorial helps! 🙂