## HC-SR04 ultrasonic sensor with arduino tutorial

### Little Theory about HC-SR04 – How Ultrasonic sensor works

HC-SR04 is a distance sensor based on ultrasonic. Distance obtained by calculating how much time required for ultrasonic to travel from transmitter to receiver. In the beginning, ultrasonic wave transmitted from a transmitter sensor and reach an obstacle. When ultrasonic wave reach obstacle, the wave will bounce back to sensor at receiver.

If we knew the speed of ultrasonic, then will obtain the distance by measure time required for wave to bounce back. We remember that distance is speed multiply time. Which is :

S = V * t

We want obtain distance from sensor to obstacle, so we need to divide the distance by 2. So it turn into

S = (V * t) / 2

Ultrasonic is sound wave, so we already knew that the speed of sound is 340m/s. Then let arduino measure time required by ultrasonic to bounce back to sensor.  Say that time required is 100 uS. So the distance will be :

S = (340*100^-6)/2

S = 0,017m or 1,7cm

or you can simplify the formula into :

S = t/2/29.1

### Apply the theory – Use HC-SR04 ultrasonics sensor with arduino

When we need distance sensor for our project, HC-SR04 can be the answer. HC-SR04 is very simple and cheap distance sensor. Now we will examine how to use this simple sensor.

Use this sketch :

```const int trig = 2;
const int ech = 3;

void setup() {
pinMode(trig, OUTPUT);
pinMode(ech, INPUT);
Serial.begin(9600);
}

void loop() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long timeRequied = pulseIn(ech, HIGH); //measure time required
int distance = (timeRequied / 2) / 29.1; //calculate the distance
Serial.println(distance);
delay(1000);
}```

The sketch above will display distance measured by sensor in Serial Monitor.

## L293D motor driver with arduino

### Introducing to L293D motor driver

If you want to control an DC motor that can run forward or reverse you can do that in many ways. But I want to tell you the simplest way to drive DC motor. In this project we will control the DC motor using single IC called L293D. This IC is powerful enough to control DC motor with low current. Before we start the wiring. We will introduce you to L293D IC first. This is the pinout :

L293D can drive up to 2 motors with single IC. Use input 1 and Input 2 to control first motor. So first motor should be connected to Output 1 and Output 2. And if you want to use second motor, connect input control to Input 3 and Input 4. And motor to Output 3 and Output 4. VCC should be connected to 5V to power up the IC. VSS is input power for motors. So if you want to use 12V motor, this pin should connect to 12V power supply. Connect 12V and 5V ground to gnd, wire all gnd. And Enable pin is used to control speed of motors using PWM. Enable 1 used to control speed of first motor, and Enable 2 for second motor.

### L293D schematic with motor example

So how we use this IC? check our example schematic below :

To Control first motor (left), you can give input from microncontroller like arduino to make motor run forward or reverse. Give Pin 2 HIGH/5V and Pin 3 LOW/0V to run motor forward. If you want to run motor reverse then give Pin 2 LOW/0V and Pin 3 HIGH/5V.  Give Pin 1 (Enable 1) PWM input to control the speed. If you use arduino UNO you can give PWM from 0-255. Mean that 255 is full speed and 0 is stopped. And if you don’t want to control the speed you can just connect Pin 1 to 5V.

Control the second motor (right) use pin 10 and 15 to give the direction, and Pin 9 to vary the speed.

Here’s example of using L293D with an arduino UNO and a DC motor

In picture above, motor power supply using a 4x AAA batteries. And 5V supply for IC given from arduino 5V.

Code example :

```const int motorPin1  = 2;  // Pin 14 of L293
const int motorPin2  = 3;  // Pin 10 of L293

void setup() {
//Set pins as outputs
pinMode(motorPin1, OUTPUT);
}

void loop() {
//This code  will turn Motor forward for 2 sec.
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(3000);
//This code will turn Motor reverse for 2 sec.
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(3000);
}
```

If you want to drive High current DC motor, you can visit here.

## How to convert resistance to voltage

Converting resistance to voltage in electronics sometimes are so important. This happens because many electronics device such as microcontroller only can read voltage as analog input signal. Analog signal then will be converted to digital using ADC. But before we talk about ADC or microcontoller, how if our sensor has resistance output instead of voltage?

#### Voltage Divider as resistance to voltage converter

The only thing we can do to make our sensor readable by microcontroller is convert resistance to voltage. Converting a resistance to voltage is far more easier than converting to other quantity. So, how we do that?

The answer is very simple. Use voltage divider !

Voltage divider has capability to divide the voltage by comparison of two resistance. You can read more details about voltage divider here.

#### Application Example

I use an LDR for a light sensor. LDR has linear resistance to the light that exposed to it surface. When light getting brighter, the resistance will decreased. Otherwise when light brightness dercreases the resistance will increases. Here’s my schematic example:

The output voltage will be :

In this example, when light density is changed, the LDR resistance will changed too and of course Voltage output will changed accordingly.

Since the voltage will change in every change of light density, then you can connect this voltage output to an microcontroller ADC.

#### Little Tips

Pick the right R2 has a little trick. You have to choose the closest one to LDR reistance range to make the voltage output difference better. For example, if your LDR resistance will vary between 800 ohm to 1k ohm then used 1k ohm resistor as R2 will be ok. But if you has LDR with resistance output vary from 8k-10k don’t use 1k as R2, using 10k as R2 will better.

## How to make Variable Voltage Regulator using LM317

Voltage Regulator is very important thing for power supply. Voltage Regulator maintain the output voltage constant as we want. Many ICs can maintain voltage output. For example LM7805 can maintain voltage at 5V. LM7809 can maintain voltage at 9V. LM7812 mantain voltage at 12V and so on. Now we will give you tutorial how to make variable voltage regulator.

The question is, can we regulate voltage at every volt we want? Yes, the answer is LM317. The datasheet says that :

The LM317 device is an adjustable three-terminal positive-voltage regulator capable of supplying more than 1.5 A over an output-voltage range of 1.25 V to
37 V. It requires only two external resistors to set the output voltage. The device features a typical line regulation of 0.01% and typical load regulation of
0.1%. It includes current limiting, thermal overload protection, and safe operating area protection. Overload protection remains functional even if the

#### How to use LM317 as Variable Voltage Regulator?

Use LM317 as variable voltage regulator is simple, you can use schematic below :

LM317 Vout equation will be : Vout = 1.25V*(1+R2/R1)

So, the key lay on R2. R2 must be variable resistor, so we can change the resistance easily. Changing the resistance will change the output.

If you still thinking that schematic above less simple, you can really make a really simple circuit using only LM317, R1 and R2. But maybe you can get a little noise from this circuit, so you can add capacitor at Vin and Vout.

Happy experimenting!

## Understanding Voltage Divider

Just like the term, voltage divider means dividing the voltage into smaller value. Sometimes this is very useful if we had only measurement instrument with limited maximum value. Such as pocket oscilloscope.

#### Understanding Voltage Divider

Dividing voltage is very simple job but very powerful. We just need two resistor to do it. Here’s the example how we do it.

Consider we have 10V voltage, using two resistor will result 5v at the output. This is how we calculate it :

Vout = Vin * R1/(R1+R2)

So if we want to divide the voltage by 3. You can simply change the R2 to 2k. So the output will be :

Vout = 10 * 1k/(1k+2k) = 3,333 V

#### Voltage Divider Application Example

In some cases, voltage divider is very useful for signal conditioning. For example, you have to sense a voltage from a power supply using an arduino or other microcontroller. The power supply can generate voltage from 0V-50V. Unfortunately, your arduino can sense voltage only from 0V-5V. That’s why voltage divider come in handy. You can just divide voltage from power supply by 10. So the output will only produce voltage between 0V to 5V and you can read using arduino ADC easily.

Beside divide the voltage, you can use voltage divider to convert resistance based sensor to voltage. For example a light dependent resistor (LDR).

Take a look at picture above. We already know that LDR will vary the resistance by how much light that exposed to it. This will make easier to read voltage output from different light brightness.

#### Voltage Divider is not for Power Supply

If you are new to electronics and think to use voltage divider for power supply then you are wrong. Why not for power supply? Voltage divider works with big resistors, so it will produce very small current. Small current cannot used for power supply. You can user linear regulator or switching power supply instead.

## How To Make Relay Driver

Relay are everywhere, almost everywhere. That’s why we have to understand how to make relay driver so we can play it.

What is a Relay?

Relay is mechanical switch, just like switch in our home that we used everyday to switch the lamp. The lamp will on or off when we press on it. Inside the switch, it just mehanical part that can connect or disconnect the conductor when we press. Like switch that works in DC or AC. Relay just do the same things.

#### Why relay need driver?

We can’t use relay directly to microcontroller such as arduino or microprocessor like raspberry pi. This is because sometimes electronics device need more voltage or current to operate than arduino pin output can supply. So we need driver, if you want to understand more why we need driver, you can read here.

#### Relay Driver Example

Now, we will make driver that can control a relay with various voltage. Here’s the schematic :

Parts needed is :

1 K resistor, NPN transistor (2N2222 or 2N3904 or other replacement), 1 diode (1N4004 or 1N4007 or other replacement).

The VCC needed is according to relay voltage. If you use 5V relay then you need 5V, if 12V then apply 12V and so on. And don’t forget to connect your relay power supply ground with microcontroller or microporcessor ground. If not, your driver will not working.

If you’re new to electronics and still confuse to read the schematic above. I will give you an hardware picture example on breadboard using arduino.

In picture above I give example of relay that using 9V VCC from battery to switch an red LED. If arduino gives HIGH output at pin 4, relay will connect LED to 9V battery. And if the output from arduino goes LOW, it will disconnect the relay and LED will turn off. You can use another load for relay like AC lamp we use everyday in home. Almost everything you can switch with relay, but pay attention to relay maximum ratings. Like maximum voltage and current. If you use relay above maximum rating, you can destroy the relay.

Here’s the example arduino sketch to blink the LED or other load using relay :

```// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(4, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(4, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(1000);                       // wait for a second
digitalWrite(4, LOW);    // turn the LED off by making the voltage LOW
delay(1000);                       // wait for a second
}```

## Understanding driver circuit / electronics driver

Driver is something that can control other electronic component, in this case something means electronics components or circuit. For example we need driver to spin motor, power up High Power LED, open or close solenoid, switch on/off AC lamp and many more.

#### Why use driver?

Driver is also a tool that needed to control other electronics component or circuit. Basicly why we need a tool to do something because it’s much easier when we use the tool or even we can’t do that thing without the tool. For example, why we need a driver to power a high power LED?

#### Driver Example

Let say we want to make blink LED with high power LED using arduino UNO. The LED has operational voltage 12V and current 500mA. How we do that? We already knew that arduino UNO has voltage output 5V and maximum current is only 40mA. If we force 5V and 40mA to power up the LED not only the LED that will not glow, but the arduino can damaged.

#### Drive a motor using transistor

Let’s take another example. Say we want to control a DC motor with arduino. The DC motor rating is 5V 1A.  Now, we knew why we need driver. The motor need 1A current while arduino only can provide 40mA. If we connect the motor directly to ardunio, we can damage our arduino. And of course the motor will difficult to spin.

Now take a look at picture below

A motor connected to arduino directly, this means motor only has supply 5V and 40mA. Beside the motor will very difficult to spin, the arduino also can damage.

At the second picture, motor connected directly to batteries. The motor will spin fast, but we can’t control with arduino of course. 😛

So the solution taken from picture above. We can see that there is a resistor and a transistor used as a driver. The transistor is used to make motor draw current from batteries, not from arduino directly. So arduino still can control the motor, but the supply for motor is taken from the batteries. So the arduino will safe. Transistor here work like switch, and the arduino like our hand. The arduino will give signal to transistor through resistor and when transistor receive the signal, it will turn on the switch.