## How to measure high temperature with k type thermocouple and MAX6675 module

In this tutorial we will see how to measure high degree temperature with Arduino by K type thermocouple. So let’s get started.

For this you will need,

1. Arduino,
2. K-Type Thermocouple,
3. Max6675 module,
4. LCD,
5. I2C,
6. Breadboard (optional) & Jumper wires.

Do connection as shown in diagram.

Now I am going to measure high temperature of this  furnace.

I inserted K type thermocouple in the furnace. This sensor is capable to measure 0 to 1024 C temperature. And I have one more device to measure high temperature. We will compare both.

##### Sketch For K type Thermocouple with MAX6675 module
```#include "max6675.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);
int ktcSO = 8;
int ktcCS = 9;
int ktcCLK = 10;
double t; //temperature in celsius
double tf; //temperature in fahrenheit
MAX6675 ktc(ktcCLK, ktcCS, ktcSO);

void setup() {
Serial.begin(9600);
lcd.begin();
delay(500);
}

void loop() {
lcd.setCursor(0,0);
lcd.print("K Type Thermocouple");
t = ktc.readCelsius(); //temperature in celsius
// uncomment below line to get temperature in fahrenheit
lcd.setCursor(0,1);
lcd.print("Temp = ");
lcd.setCursor(7,1);
lcd.print(t);
Serial.println(t);
// uncomment below line to get temperature in fahrenheit
//Serial.println(tf);

delay(1000);
lcd.clear();
}```

`ktc.readCelsius();`

This function read temperature in Celsius. And we are printing temperature value on LCD. Upload the code.

Now we can see temperature value on both screen. I think k type thermocouple is fairly accurate but showing 1 to 2 degree error at high temperature.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

https://amzn.to/2J6LJ4a (k type thermocouple and MAX6675)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2vJ3lvo   (Jumper wire)

## How to make your own Pedometer with arduino

In this tutorial we will see how to make your own pedometer which will count how many steps you have walked and also calculate calories burn and distance covered in Km. It will send that data directly to smart phone via Bluetooth. So let’s get started.

For this you will need

3. Bluetooth module (HC-05),
4. Jumper wires,
5. Lipo battery or Bluetooth battery

Do connections as shown in diagram.

##### Sketch for Pedometer
``` #include <SoftwareSerial.h>

SoftwareSerial MySerial(10, 11); // pin 10 connects to TX of HC-05 | pin 11 connects to RX of HC-05
int x,y;
int count =0;
float calories;
float distance;
void setup() {
MySerial.begin(9600);

}

void loop() {

if ( x <= 450 && y>=390)
{ count++;
//MySerial.print(count);
delay(200);
}
if ( x >= 460 && y<=370)
{
count++;
// MySerial.print(count);
delay(200);
}
calories = count * 0.035; // This value is taken depending upon my weight and height.
distance = count * 0.0006; // My step it is on average 60cm.
MySerial.print(x);
MySerial.print(",");
MySerial.print(y);
MySerial.print(",");
MySerial.print(count);
MySerial.print(",");
MySerial.print(calories);
MySerial.print(",");
MySerial.print(distance);
MySerial.print(";");
delay(150);

}```

Add SoftwareSerial library, which is inbuilt library. We are adding this library because we want serial communication on different pins. If we use pin 0 and 1 for serial communication we have to unplug the pins before uploading code and then plug it again. If we don’t unplug the pins it shows error while uploading the code. It is really annoying if we are doing trial and error with codes so that’s the reason we use softwareSerial library.

We are taking hands acceleration in x and y direction to detect steps.  Place accelerometer such that Z-axis is perpendicular to lilypad and X-axis and Y-axis is parallel to lilypad. Finding the range of acceleration values in which your hand moves is really difficult task. Because when you upload the code to lilypad you may little displace the accelerometer because of inserting and removing pins. If it is displace little bit. Accelerometer changes its values. So you need to make sure accelerometer is firmly placed on lilypad, and also make sure you don’t close the pins of lilypad where you will upload the code.

Find the range of x and y value when your hand is still. This range will not be inserted in the code. But you need to find it out because you will get an idea. When your hand is moving forward then x and y value is increasing or decreasing. Move your hand in forward direction and note the values in x and y direction.  Similarly find the range for x and y values for backward direction. Depend upon your still hand values, use <= , >= this condition.

I refer this website for calculating the calories burn, so for my height weight I found 1 step burn 0.035 calories. Similarly you can find yours. I measured my 1 step is on average 60cm. we can easily calculate distance by multiplying it with steps count. We are printing these values on mobile phone via Bluetooth. So we have to put comma (,) after every value and at the end put semi colon (;).

I am not using FTDI adapter so I will upload the code to lilypad with Arduino uno. You have to carefully remove the IC. Then connect jumper wires as shown in diagram.

Click on red link button. Now you can see the values. I have tried this pedometer with different acceleration values. I found that for actual 100 steps it was counting 90, 96, 105, 104. We can improve its accuracy by clearly defining our hand acceleration, which will require lots of trial and error. In this pedometer there is one flaw which I didn’t work on that part, it will count steps when your hand moves even when you sat on chair. I hope you will like my effort.

#### LIST OF COMPONENT BUY ONLINE:

https://amzn.to/2JtsiPs (Bluetooth module HC-05)

https://amzn.to/2IyTMlm (Battery)

http://amzn.to/2vJ3lvo   (Jumper wire)

## Bi-Directional Visitor Counter using single ultrasonic sensor with LCD

In this tutorial we are going to see how to make Bi-Directional visitor counter using single ultrasonic sensor with LCD. So let’s get started.

For this you will need

1. Arduino,
2. Ultrasonic sensor,
3. LCD,
4. I2C,
6. Jumper wire.

We are using single ultrasonic sensor for bi-directional visitor counter. Within 9 cm range it will count how many visitor enter in the room.

From 9 to 18cm range it will count how many visitor left from the room. You can also watch my previous video on bi-direction counter with seven segment LED.

Do connection as shown in diagram.

##### skecth for Bi-directional visitor counter
```#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define trigPin 13
#define echoPin 12
// Find LCD address for I2C in this tutorial it is 0x3f
LiquidCrystal_I2C lcd(0x3f, 16, 2);

int counter = 0;
int currentState1 = 0;
int previousState1 = 0;
int currentState2 = 0;
int previousState2 = 0;
int inside = 0;
int outside = 0;
void setup()
{
// initialize the LCD
lcd.begin();
//Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}

void loop()
{
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.setCursor(8, 0);
lcd.print("OUT: ");
lcd.setCursor(0, 1);
lcd.print("Total Inside: ");
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 9){
currentState1 = 1;
}
else {
currentState1 = 0;
}
delay(100);
if(currentState1 != previousState1){
if(currentState1 == 1){
counter = counter + 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
inside = inside +1;}
lcd.setCursor(4, 0);
lcd.print(inside);
if (distance > 9 && distance <= 18){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1){
counter = counter - 1;}
lcd.setCursor(14, 1);
lcd.print(counter);
outside = outside +1;}
lcd.setCursor(13, 0);
lcd.print(outside);
lcd.setCursor(14, 1);
lcd.print(counter);
if (counter > 9 || counter < 0){
lcd.setCursor(14, 1);
lcd.print(counter);
delay(100);
lcd.clear();
}
}```

Now lets come to the programming part. Add I2C library and define ultrasonic pins. If distance is less than 9 cm. counter will count 1, and “inside” which shows overall how many people enter in the room. If distance is greater than 9 cm and less than 18 cm it will minus 1 from counter. And “outside” which shows overall how many people left from the room.

```if (counter > 9 || counter < 0){

lcd.setCursor(14, 1);

lcd.print(counter);

delay(100);

lcd.clear();

}```

If we don’t add this line. Character will stuck on LCD after counter value 9.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2vmUlLw   (Ultrasonic sensor)

http://amzn.to/2vJ3lvo   (Jumper wire)

## Despacito Playing on Arduino Piano

Hello guys, in this tutorial we will see how to make Arduino piano, it may not sound like original song, because of two reason first I am not a piano artist and second is I am playing song on this hardware which is little difficult. I hope you will like my effort. So let’s get started.

For this you will need

1. Arduino,
2. Jumper wires,
3. 1K ohms resistor,
4. One copper wire,
5. Cart board,

Do connection as shown in diagram.

We are taking inputs from jumper wire to Arduino and send it to max software, which plays particular note. You need to download max 7 software.  You will also need a max patch file.

Downlaod max patch file

Copy the text in this file.

open new patcher file and paste in patcher.

After pasting the file you can see your patch like this,

This code we will upload to Arduino.

```// drawMIDI

int myKey[12];
int myOldKey[12];
int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62, 60};
int keyPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

// Pins for MIDI keys

// This is the value required to consider the key 'touched' and trigger a MIDI signal
int touchedCutoff = 36;

// This is the value required for the first part of goMIDI
int midiInfo = 144;

// This is the section that sets up serial communications
void setup(){
Serial.begin(19200); //if using hardward MIDI or HIDUINO, change to 31250
//19200 baud, requires serial to MIDI conversion on your computer

}

// This is the section that reads the capacitance and determines what to do, noteOn or noteOff
void loop(){

// only send MIDI messages when something changes
for (int i; i<12; i++) {

if (myKey[i]>touchedCutoff){
myKey[i]=127;
}
else{
myKey[i]=0;
}

if (myKey[i] != myOldKey[i]) {
myOldKey[i] = myKey[i];
goMidi(midiInfo, myNote[i], myKey[i]);
}
}
}

// The readCapacitivePin function is used to read whether the graphite strip connected to each pin is actually being pressed or not
// this part of the code is based on an open example on the Arduino playground for 'Capacitive Sensing'

// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher

volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;

if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
port = &PORTD;
ddr = &DDRD;
pin = &PIND;
}

if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
port = &PORTB;
ddr = &DDRB;
bitmask = 1 << (pinToMeasure - 8);
pin = &PINB;
}

if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
port = &PORTC;
ddr = &DDRC;
bitmask = 1 << (pinToMeasure - 13);
pin = &PINC;
}

// Discharge the pin first by setting it low and output
delay(1);

int cycles = 16000;
for(int i = 0; i < cycles; i++){
cycles = i;
break;
}
}

return cycles;
}

// This is the section that translates the touches into MIDI signals

void goMidi(int cmd, int data1, int data2) {
Serial.write(cmd); // set value stays the same
Serial.write(data1); // note number, 60 or 49 or whatever
Serial.write(data2); // velocity, keep at 127 for ON or 0 for OFF
delay(2);
}```

You can change note from here.

`int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62, 60};`

if you are getting constantly false signal from pin number 13, for example Tx LED constantly blinking then delete pin number 13 and note.

``` int myNote[] = {79, 77, 76, 72, 74, 71, 69, 67, 65, 64, 62};

int keyPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};```

upload the code, after uploading close Arduino IDE software. And open max patch file. Check max console, Arduino port is showing or not.  If it is not showing remove USB cable and close max software and again restart. After opening patch file you have to turn ON serial output. And make sure it is same port where Arduino is connected. When we press jumper wire it touches 5v line and Arduino get high signal, and it goes to max software which play particular note. You can see when I press jumper wire it plays note.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2vmSK8l  (Resistor)

http://amzn.to/2vJ3lvo   (Jumper wire)

## Arduino Tutorial 32# How to make Bi-Directional Visitor Counter

In this tutorial we will see how to make bi directional visitor counter by using 7 segment display and ultrasonic sensor. It will count how many person/object are in the system and subtract, which left from the system. So let’s get started.

For this you will need,

1. Arduino,
2. 7 Segment display,
3. 2 Ultrasonic sensor (HC-SR04)
4. Shift register 8 bit (SN74HC595),
5. 220 Ohms resistor,
6. Jumper wires,

you will like other counter tutorial

We are using shift register, which will save input/output pins on Arduino. In this tutorial we are using two ultrasonic sensor, which will not only count how many objects are entering in the system but also subtract how many object exit from the system. Sensor 1 will detect some object is entering in system, but it has to pass from sensor 2 also, so it will add 1 in counter. If object exit from system means, it has to go from sensor 2 first then sensor 1. Therefore, It will subtract 1 in counter.

Do connection as shown in diagram.

##### Sketch for Bi-Directional Visitor Counter
```const int dataPin = 3; // to 74HC595 pin 14
const int latchPin = 2; // to 74HC595 pin 12
const int clockPin = 4; // to 74HC595 pin 11
#define trigPin 13
#define echoPin 12
#define trigPin2 8
#define echoPin2 9
/* uncomment one of the following lines that describes your display
* and comment out the line that does not describe your display */
const char common = 'a'; // common anode
//const char common = 'c'; // common cathode
int button = 7;
int i = 0;
int currentState = 0;
int previousState = 0;
int currentState2 = 0;
int previousState2 = 0;

byte bits;
bool decPt = true; // decimal point display flag
case 0:
return B11111100;
break;
case 1:
return B01100000;
break;
case 2:
return B11011010;
break;
case 3:
return B11110010;
break;
case 4:
return B01100110;
break;
case 5:
return B10110110;
break;
case 6:
return B10111110;
break;
case 7:
return B11100000;
break;
case 8:
return B11111110;
break;
case 9:
return B11110110;
break;
default:
return B10010010; // Error condition, displays three vertical bars
break;
}
}
void myfnUpdateDisplay(byte eightBits) {
if (common == 'a') { // using a common anonde display?
eightBits = eightBits ^ B11111111; // then flip all bits using XOR
}
digitalWrite(latchPin, LOW); // prepare shift register for data
shiftOut(dataPin, clockPin, LSBFIRST, eightBits); // send data
digitalWrite(latchPin, HIGH); // update display
}

void setup() {
// initialize I/O pins
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(button, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}

void loop() {
decPt = !decPt; // display decimal point every other pass through loop
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
// object entering in the system
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
//delay(100);
if(currentState != previousState){
while(currentState == 1) {
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
if (distance2 <= 10){
currentState2 = 1;
}
else {
currentState2 = 0;
}
//delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1) {
i = i+1;
byte bits = myfnNumToBits(i);
myfnUpdateDisplay(bits); // display numeric digit
delay(1000); // pause for 1/2 second
}
else {
byte bits = myfnNumToBits(i);
myfnUpdateDisplay(bits); // display numeric digit
}
return;

}
}
}
// object exit from the system
if (distance2 <= 10){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
while (currentState2 == 1) {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1) {
i = i-1;
byte bits = myfnNumToBits(i);
myfnUpdateDisplay(bits); // display alphanumeric digit
delay(1000); // pause for 1/2 second
}
else {
byte bits = myfnNumToBits(i);
myfnUpdateDisplay(bits); // display alphanumeric digit
}
return;
}
}
}
}

```

Lets come to the programming part. Define shift register pins and ultrasonic sensor pins.

`byte myfnNumToBits(int someNumber)`

This function will display what number to be shown on 7 segment display by individually On/Off particular segment of LED.

`void myfnUpdateDisplay(byte eightBits)`

There are two type of connection to 7 segment display one is common anode and second is common cathode. For this tutorial we are using common anode. This function will flip all the bits using XOR and update the display .

If distance is less than 10cm on sensor 1, currentstate will become 1 and it will wait for object to pass in front of sensor 2. When it passes in front of sensor 2 it will count 1. Similarly, if object exit from system, it will pass in front of sensor 2 first, and currentstate2 will become 1, which will wait for object to pass in front of sensor 1. When it passes in front of sensor 1. It will subtract 1 from counting value. Delay time can be adjust depend upon project requirement.

As you can see when box is passing in front of sensor it is adding 1. When I passed this box in opposite direction it will simply subtract 1 from counting value.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2oPTKhB (7 segment display)

http://amzn.to/2oNcQ81 (SN74HC595N 8-Bit Shift Registers)

http://amzn.to/2vmUlLw   (Ultrasonic sensor)

http://amzn.to/2vmSK8l  (Resistor)

http://amzn.to/2vJ3lvo   (Jumper wire)

## Arduino Tutorial 31# How to make 12 hour format clock (DS1302) with Arduino

I am getting lots of request for 12 hour format clock. So, in this tutorial we will see how to make 12 hour format clock.  So let’s get started.

For this you will need

1. Arduino,
2. Real time clock (DS1302),
3. LCD (16X2),
4. I2C,
6. Jumper wires.

I am using I2C for LCD if you don’t know how to use LCD with I2C click here. Do connection as shown in diagram.

The main problem is DS1302 library is in 24-hour format but we can make it 12-hour format by just subtracting 12 hour from 24 hours and also we have to modify it further to get time in proper AM/PM format.

##### 12hour format clock DS1302 sketch
```#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x3F for a 16 chars and 2 line display // note: it may be different for your LCD please find it.
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include <DS1302.h>

// Init the DS1302
DS1302 rtc(2, 3, 4);

// Init a Time-data structure
Time t;

void setup()
{

// Set the clock to run-mode, and disable the write protection
rtc.halt(false);
rtc.writeProtect(false);

// The following lines can be commented out to use the values already stored in the DS1302
rtc.setDOW(TUESDAY); // Set Day-of-Week to TUESDAY
rtc.setTime(23, 59, 50); // Set the time to 24hr format it will automatically take time according to AM/PM
rtc.setDate(06, 2, 2018); // Set the date to Febrauray 6th, 2018
// initialize the LCD
lcd.begin();
}

void loop()
{
// Get data from the DS1302
t = rtc.getTime();

lcd.setCursor(0, 0);
if (t.date<=9){
lcd.setCursor(0,0);
lcd.print("0");
lcd.setCursor(1,0);
lcd.print(t.date, DEC);
}
else {
lcd.print(t.date, DEC);}

lcd.setCursor(3, 0);
lcd.print(rtc.getMonthStr());
lcd.setCursor(12,0);
lcd.print(t.year, DEC);
lcd.setCursor(4,1);

{if (t.hour>=12){
lcd.setCursor(13,1);
lcd.print("PM");
lcd.setCursor(4,1);
t.hour = t.hour-12;
if (t.hour== 0) {
t.hour = 12; // Day 12 PM
}
if (t.hour<=9){
lcd.setCursor(4,1);
lcd.print("0");
lcd.setCursor(5,1);
lcd.print(t.hour, DEC);
}
else {
lcd.print(t.hour, DEC);}}
else { if(t.hour==0){
t.hour=12; // Night 12 AM
}
lcd.setCursor(13,1);
lcd.print("AM");
lcd.setCursor(4,1);
if (t.hour<=9){
lcd.setCursor(4,1);
lcd.print("0");
lcd.setCursor(5,1);
lcd.print(t.hour, DEC);
}
else {
lcd.print(t.hour, DEC);}} }
lcd.setCursor(6,1);
lcd.print(":");
lcd.setCursor(7,1);
if (t.min<=9){
lcd.setCursor(7,1);
lcd.print("0");
lcd.setCursor(8,1);
lcd.print(t.min, DEC);
}
else {
lcd.print(t.min, DEC);}

lcd.setCursor(9,1);
lcd.print(":");
lcd.setCursor(10,1);
if (t.sec<=9){
lcd.setCursor(10,1);
lcd.print("0");
lcd.setCursor(11,1);
lcd.print(t.sec, DEC);
}
else {
lcd.print(t.sec, DEC);}

delay (1000);
lcd.clear();
}```

First, add I2c and DS1302 library.  Set time in 24-hour format it will take time automatically in AM/PM format.

```    if (t.date<=9){

lcd.setCursor(0,0);

lcd.print("0");

lcd.setCursor(1,0);

lcd.print(t.date, DEC);

}

else {

lcd.print(t.date, DEC);}```

I have use this code for date, hour, minute and second. What if we don’t use this?

You can see it is showing correct time but it is single digit, which is not looking like time. For this, we have added this piece of code so that it will show two digit if it is single digit. If time is greater than 12 hours means it is PM, and it will subtract 12 hours from 24 hours but there is a problem. It becomes zero at 12 o’clock so we have given “if condition” that will make it show 12 o’clock.

```    t.hour = t.hour-12;

if (t.hour== 0) {

t.hour = 12;       // Day 12 PM

}```

If hours is less than 12 hours, it will be AM. But it will show 0 hours at nights 12 o’clock.

```{ if(t.hour==0){

t.hour=12;    // Night 12 AM

}```

We have added this code so that it will show 12 o’clock in AM.

Upload the sketch, now you can see time from PM to AM. It is working fine, we can also see it is changing time from 12 o’clock to 1 o’clock.

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2gRFSiv (RTC DS1302)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2vJ3lvo   (Jumper wire)

## Arduino Tutorial 30# How to make Arduino Based RFID (RC522) Door Lock

In this tutorial, we will see how to make arduino based RFID Door Lock. And you can see When I show wrong card or wrong keychain tag, it will make sound. When I show my college ID it will open the door. For closing the door, we have to again show college ID. So let’s get started.

For this you will need

1. Arduino,
2. RFID sensor (RC522),
3. Servo motor,
4. Piezo buzzer,
5. Jumper wires,

Do connection as shown in diagram.

Before uploading our RFID door lock code. Add MFRC522 library (RFID library) in your computer. First of all, we have to find out unique identification number. For doing this

Go to file –> Examples –> MFRC522 –> READNUID

Upload the sketch. After uploading the sketch. Open serial monitor. Show the card which you want to use for Door lock. I am using my college Id card for door lock. Now we can see identification number. copy this number and come to our sketch of RFID door lock.

##### RFID door lock Sketch for 1 card access
```#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code[] = {94,73,241,250}; //This is the stored UID
int Buzzer = 6;
int activate =0;
Servo myservo;
String uidString;
void setup() {

Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
myservo.attach(5); //attach the servo to pin 5
pinMode(Buzzer,OUTPUT);
myservo.write(0); // 0 degree angle for servo

}

void loop() {
if( rfid.PICC_IsNewCardPresent())
{
}
delay(100);

}

{

Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));

// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}

Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);

int i = 0;
boolean match = true;
while(i<rfid.uid.size)
{
if(!(rfid.uid.uidByte[i] == code[i]))
{
match = false;
}
i++;
}

if(match)
{ if (activate==0){
myservo.write(90); // 90 degree angle for servo (Door will open)
Serial.println("\nDoor is open");
delay(1000);
activate = activate+1;
}
else
{ myservo.write(0); // 0 degree angle for servo (Door will close)
activate = 0;
Serial.println("\nDoor is close");
delay(1000);
}
}

else
{
tone(6,400,1000);
Serial.println("\nUnknown Card");
}

// Halt PICC
rfid.PICC_HaltA();

// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}```

Identification number which we have copied earlier, paste here.

`int code[] = {94,73,241,250}; //This is the stored UID`

I have given 0 degree angle for door lock position. readRFID will read the card number. If card  number match with our card number door will open and for door opening 90 degree angle is given. Activate will become 1. You can change these angles according to your door lock opening and closing position. If we again show same card it will close the door. If card number does not match with our card number, buzzer will make sound for 1 second. You can see when I show unknown card or keychain tag it makes sound. When I show right card servo will rotate to 90 degree and when I again show same card servo will rotate back to 0 degree angle.

if you want to add more than one card for access. you can use this code

##### RFID door lock Sketch for 2 card access
```#include <MFRC522.h>
#include <SPI.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

MFRC522::MIFARE_Key key;

int code1[] = {94,73,241,250}; // access card 1
int code2[] = {230,112,166,172}; // access card 2
int Buzzer = 6;
int activate =0;
Servo myservo;
String uidString;
void setup() {

Serial.begin(9600);
SPI.begin(); // Init SPI bus
rfid.PCD_Init(); // Init MFRC522
myservo.attach(5); //attach the servo to pin 5
pinMode(Buzzer,OUTPUT);
myservo.write(0); // 0 degree angle for servo

}

void loop() {
if( rfid.PICC_IsNewCardPresent())
{
}
delay(100);

}

{

Serial.print(F("\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));

// Check is the PICC of Classic MIFARE type
if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
Serial.println(F("Your tag is not of type MIFARE Classic."));
return;
}

Serial.println("Scanned PICC's UID:");
printDec(rfid.uid.uidByte, rfid.uid.size);

uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);

int i = 0;
boolean match = true;
while(i<rfid.uid.size)
{
if(!(rfid.uid.uidByte[i] == code1[i]) && !(rfid.uid.uidByte[i] == code2[i])) // add card here with "&&" and also change code number
{
match = false;
}
i++;
}

if(match)
{ if (activate==0){
myservo.write(90); // 90 degree angle for servo (Door will open)
Serial.println("\nDoor is open");
delay(1000);
activate = activate+1;
}
else
{ myservo.write(0); // 0 degree angle for servo (Door will close)
activate = 0;
Serial.println("\nDoor is close");
delay(1000);
}
}

else
{
tone(6,400,1000);
Serial.println("\nUnknown Card");
}

// Halt PICC
rfid.PICC_HaltA();

// Stop encryption on PCD
rfid.PCD_StopCrypto1();
}

void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}```

#### LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2DKDZyI (RFID RC522)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2uKfEDf  (Piezo buzzer)

http://amzn.to/2weYm4R (servo motor)

## Arduino Tutorial 29# How to control 240V/120V light bulb with solid state relay

In this tutorial we will learn How to control 240V/120V light bulb with solid state relay. We will also see what is difference between contact relay and solid state relay. So let’s get started.

For this you will need

1. Arduino,
2. Solid state relay,
3. Jumper wires,
4. Light bulb,
5. Bulb holder, wires and plug.

Before starting this tutorial I want to give you a warning. Doing incorrectly can cause you serious injury or damage your Arduino. If you are not sure don’t do this.

Contact relay which actuate by means of electromagnetic force. When contact happen it makes “click” sound. Mechanical contact takes little large time to move. And also there is a chance to wear out the mechanical part.

Where as in solid state relay works on optocoupler. Which does not require any mechanical moving part. Therefore solid state relay have fast switching speed than contact relay.

This solid state relay is triggered by 3 to 32VDC. And output voltage is up to 330VAC.

Do connection as shown in diagram.

```int led_light = 13;
void setup() {
pinMode(led_light, OUTPUT);

}

void loop() {
digitalWrite(led_light, HIGH);
delay(2000);
digitalWrite(led_light, LOW);
delay(2000);
}

```

Upload blink sketch from examples and change delay time to 2000 millisecond for on and off. After uploading the code turn on extension board. You can see it is on for 2 second and off for 2 second. Now I connected CFL bulb it works fine. But when I connect LED bulb at LOW signal it flicker for few millisecond.  You can control fan, light any home appliance with this relay.

##### Components list to buy online:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2AW1iYb (Solid State Relay)

http://amzn.to/2vJ3lvo   (Jumper wire)

For more videos subscribe the channel

## Arduino Tutorial 28# How to control 240V /120V light bulb from 5V arduino using relay module

Posted 1 CommentPosted in Arduino

In this tutorial we will see how to control 240V/120V light bulb from 5V Arduino using Relay module. And we will turn on and off light by using blinking code. So, let’s get started.

Warning!

Before starting this tutorial, I want to give you a warning. Since we are using 240V/ 120V line voltage you should take necessary precaution when dealing with high voltage. Doing this incorrectly can cause you serious injuries or death. Or damage your Arduino/Relay. For any action I am not responsible for that.

For doing this project you will need

1. Arduino,
2. Relay module,
3. Diode (1N4007), for extra protection.
5. 240V/120V light bulb ,
6. Bulb holder, wire and plug.

I am using 2 channel relay module. you can use single or 2 channel relay it doesn’t matter.

I have putted diode between Arduino and relay. It will give Arduino extra protection by allowing current to flow only in one direction. One wire is cut between holder and plug. One end is connected to common pin (COM) and other end is connected to normally closed pin (NC). Do connection as shown in diagram. Use extension board if something could go wrong, you can switch it off directly.

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

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

Change delay time to 2000 millisecond for on and off. Upload the sketch. After uploading the sketch, turn on extension board. Now we can see our 240v light is turn on for 2 second and off for 2 second.

##### Components list to buy online:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2y8horo (Relay Module)

http://amzn.to/2AfkqyH (Diode 1N4007)

http://amzn.to/2vJ3lvo   (Jumper wire)

For more videos subscribe the channel

## Arduino Tutorial 27# How to control 12V LED and motor with 5V Arduino using Transistor TIP 122

In this tutorial we will see how to control 12V LED and motor with 5V Arduino by using Transistor TIP122. And we are using transistor as a switch and also controlling led and motor with plus width modulation. We will also see how to calculate base current resistor. So let’s get started.

For this you will need

1. Arduino,
2. Transistor (TIP 122),
3. Diode (1N4007) If you are controlling motor,
4. 12V battery,
5. 2K ohms Resistor,
6. Jumper wires,
8. 12V LED, (If you are controlling LED)
9. 12V Motor (If you are controlling Motor)

##### How to find base current resistor?

First we need to calculate base resistor for transistor. Using this method you can find any transistor base resistance. Download data sheet of transistor TIP 122. Now goto page number 3 and in second figure we can see equation on top right corner Ic=250 X Ib,

So we need to check how much load current is require to run a motor.  My motor require 0.44Amp current. It may be printed on motor or it is written in motor specification. So my Ic value will be 0.44Amp. Now calculate Ib which is equal to 1.76mA.

Ib = 0.44/250

Ib= 1.76mA

Look at figure 2 again now we know collector current. Check 0.44 value in figure 2 and Match with Vbe saturation. Its value is around 1.375 V.

Vbe= 1.375 V

It means that Arduino has voltage drop of (5-1.375) which is equal to 3.625V. Now according to ohms law R = V/Ib.

3.625/1.76×10-3 = 2059.65 ohms

So we get 2059 ohms as a base current resistor value.

but I am using 2.2K ohms resistor. You can calculate base resistor according to your load current.

To control 12V LED we need to connect our circuit like this.

After doing connection. Open Arduino IDE software,

goto file > Examples > Basics > blink.

Upload led blink sketch. Now we can see we are using transistor as a switch it will turn on and off 12V led for 1 second.  Now again goto Arduino IDE software

goto file > Examples > Basics > fade,

upload fade sketch from examples. Change pin number to 9. Now we can see it is fade on and off. We are controlling this with plus width modulation.

Now we will control motor. Just add diode across the motor.

Now upload the blink sketch. I have given 1 second delay for ON and 3 second delay for OFF. So that we can see clearly motor is stop spinning. Now we will control motor with plus width modulation. Upload the sketch of fade from examples. Now we can see it is varying its speed in every 300 millisecond.

##### Components list to buy online:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2zPbscy (Transistor TIP 122)

http://amzn.to/2nd75Ch (12V Battery)

http://amzn.to/2jx6031 (12V LED strip)

http://amzn.to/2jwb0Vv (12V Motor )

http://amzn.to/2AfkqyH (Diode 1N4007)