## 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 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!

## 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
}```

## How to use Thermocouple type K (MAX6675) with arduino

Thermocouple is powerful temperature sensor. In this case, using thermocouple type K and MAX6675 can read temperature in range 0ºC to 1024ºC,  this sensos also waterproof. So it’s okay if you want place this sensor in water for measure water temperature. Beside that, thermocouple has tons of module that you can get easily, not like RTD which usually we have to make signal conditioning by ourselves. Sometimes it is really annoying.

And, let’s get dirty!

First wire your max6675 to arduino. In this example I use arduino UNO.

Pin SCK -> 6

Pin CS -> 5

Pin SO -> 4

Pin VCC-> 5V

Pin GND -> GND

Gambar koneksi

Double check your wiring, make sure everythings is connected correctly.

Now, it’s time to code.

Before we start code, to make our life easier we can use a library. As example we can use a library from adafruit. You can download here.

After you download the library, copy the folder to your arduino directory/library. It is usually in C:\program files (x86)\arduino\library. If your arduino software is running when copying the library. Make sure you restart the software after add the library to let arduino recognized new library.

Hmmm now it’s real time to code. Haha

You can use code below, or you can find this code at library example.

``` #include "max6675.h"

int thermoDO = 4;
int thermoCS = 5;
int thermoCLK = 6;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;

void setup() {
Serial.begin(9600);
// use Arduino pins
pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);

Serial.println("MAX6675 test");
// wait for MAX chip to stabilize
delay(500);
}

void loop() {
// basic readout test, just print the current temp
Serial.print("C = ");