How to take arduino sensor data to excel sheet

Arduino Tutorial #26 How to take Arduino sensor data to excel sheet.

Posted Leave a commentPosted in Arduino

In this tutorial we are going to see how to take arduino sensor data to excel sheet and plot it for analysis. So let’s get started.

For this you will need a software Tera Term. Which will take Arduino data to excel sheet.  You can take any sensor data with this software. Download link for software Tera Term.  For this tutorial we are going to make temperature go high with hot air of hair dryer and sensor data will be plotted in excel sheet. If you want to know more about temperature sensor with Arduino click here.  After downloading software tera term. Install it. Now you can see its icon on desktop but before opening it go to Arduino IDE.

Sketch we are using for this experiment.

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2 // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)


DHT dht(DHTPIN, DHTTYPE);

void setup() {
 Serial.begin(9600);
 Serial.println("Date & Time, Humidity %, Temperature *C, Temperature *F");

dht.begin();
}

void loop() {
 // Wait a few seconds between measurements.
 delay(1000);

// Reading temperature or humidity takes about 250 milliseconds!
 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
 float h = dht.readHumidity();
 // Read temperature as Celsius (the default)
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit (isFahrenheit = true)
 float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t) || isnan(f)) {
 Serial.println("Failed to read from DHT sensor!");
 return;
 }


 Serial.print(",");
 Serial.print(h);
 Serial.print(",");
 Serial.print(t);
 Serial.print(",");
 Serial.println(f);

}

And add this line in setup function.

 Serial.println("Date & Time, Humidity %, Temperature *C, Temperature *F");

Depend upon your sketch you can decide how many numbers of columns you require. We want 4 columns first column is for date & time. But make sure each column is separated with comma (,). Then second column is for humidity then temperature in Celsius & temperature in Fahrenheit. And also make sure it is printed in new line.

 Serial.print(",");
 Serial.print(h);
 Serial.print(",");
 Serial.print(t);
 Serial.print(",");
 Serial.println(f);

We are adding comma before humidity because first column is for date and time it will be generated by tera term software. So each data is separated by comma. Make sure it is in same line. In last line it is println. Now open the tera term software.

Tera Term Serial
Tera Term Serial

Select serial and port. Goto file and click on log.

Tera Term log
Tera Term log

Check the box timestamp because we want date and time also. It will ask where we want to save this log file.

Tera Term save log
Tera Term save log

I am saving it on desktop. Make sure you save it with .csv file. So it will create excel sheet. After saving log file press reset button on Arduino. Now I am making temperature go high by hot air of hair dryer. After taking readings you can close the tera term software. Goto desktop and open file. In excel sheet select all column and goto insert then chart and plot all three data in one graph.

Arduino sensor data is plotted in excel sheet
Arduino sensor data is plotted in excel sheet

You can plot only one data at a time just select two column and plot it.  This data can use for further analysis.

Single data graph for arduino sesnor data to excel sheet
Single data graph for arduino sesnor data to excel sheet
Components list to buy online:

http://amzn.to/2vqIKJP  (DHT22)

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

http://amzn.to/2wxPmWz  (Breadboard)

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

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

For more videos subscribe the channel

Till than keep learning keep making. 🙂

 

How to make arduino alarm clock

Arduino Tutorial #25 How to make Arduino Alarm Clock

Posted Leave a commentPosted in Arduino

In this tutorial we are going to see how to make arduino alarm clock. To set alarm you don’t need to go on Arduino IDE software. You can set alarm just by pressing pushbuttons. So let’s get started.

For this you will need

  1. Arduino,
  2. Real time clock (DS1302),
  3. Piezo Buzzer,
  4. LCD,
  5. I2C,
  6. 4 pushbuttons,
  7. Four 1K ohms resistors,
  8. Jumper wires,
  9. Breadboard.

Do connection as shown in diagram.

 

Circuit diagram for arduino alarm clock
Circuit diagram for arduino alarm clock

 

Sketch for arduino alarm clock
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include <DS1302.h>
int Hour;
int Min;
int pset = 8; // pushbutton for setting alarm
int phour = 9; // pushbutton for hour
int pmin = 10; // pushbutton for minutes
int pexit = 11; // pushbutton for exit of set alarm
int buzzer = 6;
int h;
int m;
int buttonforset = 0; // pushbutton state for setting alarm
int buttonforhour = 0; // pushbutton state for hour
int buttonformin = 0;// pushbutton state for minutes
int buttonforexit = 0; // pushbutton state for exit of set alarm
int activate=0;
 
Time t;

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



void setup()
{
 pinMode(pset, INPUT);
 pinMode(phour, INPUT);
 pinMode(pmin, INPUT);
 pinMode(pexit, INPUT);
 // Set the clock to run-mode, and disable the write protection
 rtc.halt(false);
 rtc.writeProtect(false);


 // Setup LCD to 16x2 characters
 lcd.begin();

// The following lines can be commented out to use the values already stored in the DS1302
 //rtc.setDOW(SATURDAY); // Set Day-of-Week to FRIDAY
 //rtc.setTime(10, 0, 0); // Set the time to 12:00:00 (24hr format)
 //rtc.setDate(11, 11, 2017); // Set the date to August 6th, 2010
}

void loop()
{
 if (activate == 0) {

// Display time on the right conrner upper line
 lcd.setCursor(0, 0);
 lcd.print("Time: ");
 lcd.setCursor(6, 0);
 lcd.print(rtc.getTimeStr());
 
 // Display abbreviated Day-of-Week in the lower left corner
 //lcd.setCursor(0, 1);
 //lcd.print(rtc.getDOWStr(FORMAT_SHORT));
 
 // Display date in the lower right corner
 lcd.setCursor(0, 1);
 lcd.print("Date: ");
 lcd.setCursor(6, 1);
 lcd.print(rtc.getDateStr());
 t = rtc.getTime();
 Hour = t.hour;
 Min = t.min;
 buttonforset = digitalRead(pset);
 } // setting button pressed
 if (buttonforset == HIGH) {
 activate =1;
 lcd.clear(); }
 while(activate== 1){
 lcd.setCursor(0,0);
 lcd.print("Set Alarm");
 lcd.setCursor(0,1);
 lcd.print("Hour= ");
 lcd.setCursor(9,1);
 lcd.print("Min= ");
 buttonforhour = digitalRead(phour); // set hour for alarm
 if (buttonforhour == HIGH){
 h++;
 lcd.setCursor(5,1);
 lcd.print(h);
 if (h>23){
 h=0;
 lcd.clear(); }
 delay(100); 
 }
 buttonformin = digitalRead(pmin); // set minutes for alarm
 if (buttonformin == HIGH){
 m++;
 lcd.setCursor(13,1);
 lcd.print(m);
 if (m>59){
 m=0; 
 lcd.clear();}
 delay(100); 
 }

lcd.setCursor(5,1);
 lcd.print(h);
 lcd.setCursor(13,1);
 lcd.print(m);
 buttonforexit = digitalRead(pexit); // exit from set alarm mode
 if (buttonforexit == HIGH){
 activate = 0;
 lcd.clear();
 }
 }
 
 if (Hour== h && Min== m) {
 tone(6,400,300);}
 delay (500);
 }

 

Include I2C and real time clock library. Define pushbutton and buzzer pin number. Define pushbutton states. Define pin mode for pushbuttons. You can set the correct time from here.

// The following lines can be commented out to use the values already stored in the DS1302
 //rtc.setDOW(SATURDAY); // Set Day-of-Week to FRIDAY
 //rtc.setTime(10, 0, 0); // Set the time to 12:00:00 (24hr format)
 //rtc.setDate(11, 11, 2017); // Set the date to August 6th, 2010

 

If you want to know more about real time clock with Arduino. You can see Arduino clock on my previous tutorial. In loop function, initially we already set activate equal to zero. It means it will show date and time. If we pressed setting button it will activate 1. By this we enter in set alarm mode.  Now we can press button to set hours and minutes for alarm. If hour is greater than 23 it will make hours equal to zero, similarly if minutes is greater than 59 it will make minutes equal to zero. If we press exit button it will activate 0 and show current time. If Hours and Minutes matched with what we set, buzzer make sound through pin number 6 at 400 frequency for 300 millisecond. Now we can see I am setting alarm by pressing button. When it reaches its time buzzer make sound.

 

LIST OF COMPONENT BUY ONLINE:

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

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

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

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

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

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

http://amzn.to/2wxPmWz  (Breadboard)

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

TILL THEN KEEP LEARNING KEEP MAKING 🙂

How to connect I2C with LCD

Arduino Tutorial #24 How to connect I2C with LCD.

Posted 3 CommentsPosted in Arduino

in this tutorial we will learn how to connect I2C with LCD. It will reduce 4 input/output ports on Arduino board.  And wiring is much simpler and easier to connect. So let’s get started.

For this you will need

  1. Arduino,
  2. I2C,
  3. Breadboard (optional if you solder I2C with LCD),
  4. Jumper wires.
I2C
I2C

This is I2C serial interface adapter. We can adjust contrast of LCD by this potentiometer. We can solder this directly on LCD. But we have already solder the LCD so in this tutorial we are going to connect I2C and LCD by breadboard. Do connection as shown in diagram.

 

Circuit diagram for I2C with LCD
I2C with LCD circuit diagram
I2C with LCD circuit diagram

 

Download library for Liquid_Crystal_I2C. After downloading it, unzip it and change the name of folder to LiquidCrystal_I2C. And copy that folder and paste it to Arduino libraries.  Before uploading any sketch first we need to find out its I2C address. I2C scanner for finding address. Copy that code and paste it. Compile & Upload the sketch. Go to serial monitor. Now you can see its address. Copy that address.

And goto examples –> LiquidCrystal_I2C –>  hello world.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
 // initialize the LCD
 lcd.begin();

// Turn on the blacklight and print a message.
 lcd.backlight();
 lcd.print("Hello, world!");
}

void loop()
{
 // Do nothing here...
}

Change lcd address to previously copied address.

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);

Compile and upload the sketch, now you can see hello world on the screen. If it is not showing anything on screen you can adjust contrast of lcd by adjusting potentiometer.

Now goto examples –> LiquidCrystal –> display

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display

LiquidCrystal_I2C lcd(0x27, 16, 2);

Copy this code and instead of

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

this code paste this I2C library to this sketch and delete 16X2 in lcd.begin.  Compile and upload the sketch now you can see it is turn on & off display in each 500 millisecond.

 

LIST OF COMPONENT BUY ONLINE:

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

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

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

http://amzn.to/2wxPmWz  (Breadboard)

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

TILL THEN KEEP LEARNING KEEP MAKING 🙂

 

Arduino Real Time Clock with Real Time Temperature

Arduino Tutorial #23 How to make Arduino Real Time Clock with Real Time Temperature.

Posted 1 CommentPosted in Arduino

In this tutorial we will see how to make Arduino real time clock and it will also show real time temperature on LCD display. So let’s get started. For this you will need

  1. Arduino,
  2. Real time clock (DS1302),
  3. DHT22 sensor,
  4. LCD,
  5. Potentiometer,
  6. Jumper wires,
  7. Breadboard,
  8. And Resistors.

 

Circuit diagram for Real time clock with LCD
Circuit diagram for Real time clock with LCD

Do connection as shown in diagram. You can use I2C serial interface adapter to reduce wires in LCD. And make sure DHT sensor connected this 5V line before the real time clock otherwise it will not work.

 

Sketch for DS 1302 RTC with LCD
#include "DHT.h"
#define DHTPIN 10
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE); 
#include <LiquidCrystal.h>
#include <DS1302.h>

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

// Init the LCD
LiquidCrystal lcd(12, 11, 8, 7, 6, 5);

void setup()
{
 // Set the clock to run-mode, and disable the write protection
 rtc.halt(false);
 rtc.writeProtect(false);
 dht.begin();
 
 // Setup LCD to 16x2 characters
 lcd.begin(16, 2);

// The following lines can be commented out to use the values already stored in the DS1302
 rtc.setDOW(MONDAY); // Set Day-of-Week to FRIDAY
 rtc.setTime(12, 11, 0); // Set the time to 12:00:00 (24hr format)
 rtc.setDate(30, 10, 2017); // Set the date to August 6th, 2010
}

void loop()
{
 // Display time on the left conrner upper line
 lcd.setCursor(0, 0);
 lcd.print(rtc.getTimeStr());
 
 // Display abbreviated Day-of-Week in the lower left corner
 lcd.setCursor(0, 1);
 lcd.print(rtc.getDOWStr(FORMAT_SHORT));
 
 // Display date in the lower right corner
 lcd.setCursor(6, 1);
 lcd.print(rtc.getDateStr());
 float t = dht.readTemperature();
 // Display tempreture right side corner on the upper line 
 lcd.setCursor(9, 0);
 lcd.print(t);
 lcd.print("*C");

// Wait one second before repeating 
 delay (1000);
}

Now in programming part include DHT, real time clock and liquid crystal library. Download DHT sensor library and real time clock (DS 1302) library. If you are interested in to little bit more about DHT sensor you can check measure temperature and humidity with Arduino. Define DHT pin number and DHT type. Initialize real time clock library and LCD library. In setup function change the Day, Time, and Date to current time. In loop function it will print the day, time, date and temperature to set cursor. After changing date, time upload the sketch you can see it is showing current time but when we unplug the power and re-plug it. It is starting from where we set the clock. But we don’t want to set clock for every time.

 

 // The following lines can be commented out to use the values already stored in the DS1302
 rtc.setDOW(MONDAY); // Set Day-of-Week to FRIDAY
 rtc.setTime(12, 11, 0); // Set the time to 12:00:00 (24hr format)
 rtc.setDate(30, 10, 2017); // Set the date to August 6th, 2010

 

So we need to comment out this lines. This will set clock to run mode. And now you can see when I unplug the power and re-plug it. It is showing exact time. It is not starting over again where we have set it. Because when it was not powered it was running on battery. Even when I press reset button it is not affecting the clock. Now you can see real time clock and real time temperature on LCD display.

Remember this two step

  1. update time and upload sketch.
  2. then comment out the lines and  again upload sketch.

In next tutorial I will make video on Arduino alarm clock. Subscribe my Youtube channel to get latest update.

 

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/2vqIKJP  (DHT22)

http://amzn.to/2vYitnO (Potentiometer)

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

http://amzn.to/2wxPmWz  (Breadboard)

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

TILL THEN KEEP LEARNING KEEP MAKING 🙂

Ultrasonic sensor counter with LCD display

Arduino Tutorial #22 Ultrasonic sensor counter with LCD display

Posted 2 CommentsPosted in Arduino

In this tutorial we will see how to use ultrasonic sensor as a counter and output will be shown on LCD display. It will count number of times object passes in front of sensor. So let’s get started. For this you will need

  1. Arduino,
  2. Ultrasonic sensor,
  3. LCD display,
  4. Potentiometer (for adjusting contrast of LCD),
  5. Breadboard,
  6. 4.7 K ohms Resistor,
  7. Jumper wires.

 

 Ultrasonic sensor counter with LCD circuit diagram
Ultrasonic sensor counter with LCD circuit diagram

 

Do connection as shown in diagram. Provide separate ground for ultrasonic sensor. And connect VCC pin near to 5V pin. Otherwise it will not work and sensor will give constantly increment values even you are not doing anything with sensor.

 

Sketch for Ultrasonic sensor counter with LCD
 #include <LiquidCrystal.h>
 #define trigPin 13
 #define echoPin 8
 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 int counter = 0;
 int currentState = 0;
 int previousState = 0;
 
 void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 lcd.begin(16, 2);
 lcd.setCursor(4, 0);
 lcd.print("counter");
 }
 
 void 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;
 if (distance <= 10){
 currentState = 1;
 }
 else {
 currentState = 0;
 }
 delay(200);
 if(currentState != previousState){
 if(currentState == 1){
 counter = counter + 1;
 lcd.setCursor(4,1);
 lcd.print(counter);
 }
 }
 }

This code is already explain in previous tutorial measuring distance with ultrasonic and ultrasonic as a counter. In this tutorial we have added LCD for output. Now let’s come to the programming part. Include liquid crystal library. Define trigger and echo pins. Initialize liquid crystal library. Define variables for detecting changes in state.  In setup function declare pin mode for trigger and echo pin. Set cursor at little bit right side and print counter text on LCD. In loop function this will determine the distance. If any object comes within range of 10 cm. current state will be equal to 1. If current state equal to 1 it will count 1. LCD cursor is set to second row for printing counting values.

You can see when I move box in front of ultrasonic sensor it counts how many time box passes.

 

LIST OF COMPONENT BUY ONLINE:

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

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

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

http://amzn.to/2vYitnO (Potentiometer)

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

http://amzn.to/2wxPmWz  (Breadboard)

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

TILL THEN KEEP LEARNING KEEP MAKING 🙂

 

PIR sensor counter with LCD display

Arduino Tutorial #21 How to make counter using PIR sensor with LCD display

Posted 2 CommentsPosted in Arduino

In this tutorial we will see how to make counter using motion detection sensor (PIR) and output will be shown on LCD display. It will count how many times object passed over it. So let’s get started. For this you will need

  1. Arduino,
  2. LCD display 16X2,
  3. PIR sensor,
  4. Potentiometer (For adjusting contrast of LCD),
  5. LED,
  6. Resistors,
  7. Breadboard,
  8. Jumper wire.

 

Back side of LCD display
Back side of LCD display

 

At the back side of LCD you can see, which pin number of LCD is for VCC, VSS etc.

circuit diagram for PIR sensor counter with LCD
circuit diagram for PIR sensor counter with LCD

 

Do connection as shown in diagram. I have used 2.2K ohms resistor for LCD backlight. Depending upon your need of brightness of display you can increase or decrease the resistance. If LED is not working after doing connection as per shown in diagram, try to add separate ground for LED.

 

Sketch for PIR counter with LCD:
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int ledPin = 13;
int pirPin = 10; 
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(pirPin, INPUT); // declare sensor as input
 lcd.begin(16, 2);
 lcd.setCursor(4, 0);
 lcd.print("counter");
}
void loop(){
val = digitalRead(pirPin); // read PIR sensor input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
currentState = 1;
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
currentState = 0;
// We only want to print on the output change, not state
pirState = LOW;
}
}
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.setCursor(4,1);
lcd.print(counter);
delay(200);
}
}
}

 

I have made this tutorial already but in this video we have added LCD so that we will get output on LCD not on serial monitor. This code is already explain in tutorial motion detection sensor as a counter. I will cover this code briefly here. Include Liquid crystal library which is preinstalled in Arduino IDE. Initialize library with the numbers of interface pins. You can see in circuit diagram 2,3,4,5 and 11, 12 pins are connected to LCD. Define LED and PIR pin numbers. Here we are declaring parameter such as current state , previous state, val which will tell Arduino motion detected. In setup function declare pinmode of LED and PIR. Lcd.begin will define dimension of LCD in this case it is 16 columns and 2 rows. I am Setting cursor at little bit right side. That’s why I took 5th column but its index number is 4. Similarly for first row its index number is 0. In loop function Lcd.print will print the text counter. If PIR sensor get motion detection it will turn on LED. Otherwise LED will be off. We want to count when there is motion detection therefore if currentsate is equal to 1 it will count 1.  Initially counter value is set to zero. We have set cursor for counter value in second row. Lcd.print will print the counter value. Make sure it will be not in double quote otherwise you will get counter text as an output.

You can see when I move my hand over sensor it will count how many times my hand pass over it.

LIST OF COMPONENT BUY ONLINE:

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

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

http://amzn.to/2vqnDHc   (PIR sensor)

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

http://amzn.to/2vSpUON   (LED)

http://amzn.to/2wxPmWz  (Breadboard)

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

KEEP LEARNING KEEP MAKING 🙂

Password lock on door using arduino

Arduino Tutorial #20 Password lock on door using arduino

Posted Leave a commentPosted in Arduino

In this tutorial we will see how to make door lock with password and when someone try wrong password buzzer will make sound.  So let’s get started. For this you will

  1. Arduino,
  2. Keypad,
  3. Servo motor,
  4. Buzzer,
  5. Breadboard (optional),
  6. Jumper wire,
  7. Steel wire to connect servomotor and sliding door lock.

 

Circuit diagram for password door lock using arduino
circuit diagram for password lock on door
circuit diagram for password lock on door

 

Do connection as shown in diagram.

Sketch for door lock password using arduino
#include <Servo.h>
#include <Keypad.h>

//Variables
int min_Ang = 0; 
int max_Ang = 135; 
int character = 0;
int activated =0;
char Pass_char[4] = {' ', ' ', ' ', ' '}; 
Servo myservo;
int buzzer=10;



//Keypad config
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
 {'1','4','7','*'},
 {'2','5','8','0'},
 {'3','6','9','#'},
 {'A','B','C','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
 myservo.attach(11); //attach the servo to pin 11
 pinMode(buzzer,OUTPUT); 
 myservo.write(min_Ang); 
 
}
 
void loop(){

char customKey = customKeypad.getKey(); //this function reads the presed key
 
 if (customKey){

if (character ==0)
 { 
 Pass_char[0]= customKey; 
 }

if (character ==1)
 { 
 Pass_char[1]= customKey; 
 }

if (character ==2)
 { 
 Pass_char[2]= customKey; 
 }

if (character ==3)
 { 
 Pass_char[3]= customKey; 
 activated=1;
 }
 
 character=character+1;
 }

if (activated == 1)
 {
// you can change your password here (current passowrd is 1357)

if(character==4 && Pass_char[0]=='1' && Pass_char[1]=='3' && Pass_char[2]=='5' && Pass_char[3]=='7' )
 {
 myservo.write(max_Ang);
 activated = 2;
 delay(1000);
 }
 else
 {

digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 digitalWrite(10,HIGH);
 delay(400);
 digitalWrite(10,LOW);
 delay(100);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' ';
 }
 }
 if (activated == 2)
 {
 if(customKey == 'C' )
 {
 myservo.write(min_Ang);
 activated = 0;
 character=0;
 Pass_char[0]= ' ';
 Pass_char[1]= ' '; 
 Pass_char[2]= ' '; 
 Pass_char[3]= ' '; 
 }
 } 
}

 

Let’s come to the programming part. We are not using single wire connection for keypad that’s why in this sketch we are using keypad library. Which you can download by given link keypad library. After downloading unzip it. Copy that folder and paste it to directory of Arduino libraries. Now you are ready to go with keypad library.

Include keypad and servo library. Define integer minimum angle which is locked position at 0 degree angle.  Maximum angle is open position at 135 degree angle. You can change this angle according to your door open position. Initially character and activated is zero. Now we are defining character array which contain four element. Which is our password initially it is blank space. Create servo to myservo. Buzzer pin inserted at pin number 10. keypad matrix is taken from keypad library and you can adjust row & column pin number if it is not working correctly. In setup function Myservo is attached to pin number 11. Set pin mode of buzzer as an output. Myservo.write will rotate servo to zero degree angle. In loop function this function will read the pressed key. If any key is pressed it will check character is zero, if it is zero it will assign that pressed character to password character index number 0. Similarly for password character index number 1, 2, 3. It will increase character number on each keypress. When password character index number 3 is completed it will activate 1. If entered password is matched with exact number and sequence, my servo write will rotate the servo and open the door. In this case password is 1357. Now it will activate 2. If C character is pressed it will again lock the door and reset the activated and character equal to zero. It also reset entered password which is containing in form of array in password character. If password is not matched with our password, it will execute code written in else bracket. Buzzer will make sound. It will again reset all parameter activated and character is equal to zero.

You can see when I entered wrong password it makes sound and when I entered right password it rotate the servo 135 degree angle. It comes again to zero degree angle when I press C button.

LIST OF COMPONENT BUY ONLINE:

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

http://amzn.to/2wjd7p4 (Keypad 4×4)

http://amzn.to/2wxPmWz  (Breadboard)

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

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

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

KEEP LEARNING KEEP MAKING 🙂

How to connect keyboard with arduino using single wire

Arduino Tutorial #19 How to connect keyboard with Arduino using single wire

Posted Leave a commentPosted in Arduino

Hi guys in this tutorial we will learn how to connect keyboard with Arduino using single wire. And sketch we are using in this, is not depend upon the library.  So let’s get started.

For this you will need

  1. Arduino,
  2. 4 X 4 keypad,
  3. Breadboard,
  4. Jumper wires,
  5. Assorted Resistors.

 

keypad pin Rows and Columns
keypad pin Rows and Columns

Let us first understand how keyboard works. It has four rows and four column. The leftmost pins are for Rows and rightmost for Columns. For example if you pressed five number on keypad it will make connection between column 2 and row 2.  Similarly if you pressed number 8 it will make connection between column 2 and row 3. This column and row connection gives signal to Arduino which button is pressed. In this tutorial we will see how to connect keyboard with single wire in Arduino.

keypad connection when 5 pressed
keypad connection when 5 pressed

 

keypad connetion when 8 pressed
keypad connetion when 8 pressed

 

Circuit diagram for keyboard connection using single wire
Circuit diagram for keyboard connection using single wire

Do connection as shown in diagram.  You can see there are various resistor is used. Because we want to connect with signal wire. So we need to differentiate output between each button connection. You can use any resistor but make sure its output value should not matched with any other keypad button value. I will show you exactly what I am talking about later in this lesson. If you are using different resistors you need lots of trial and error for getting particular output for each keypad character. When you pressed number 1 on keypad it will connect row 1 and column 1. From our circuit diagram we can see current is going through two resistor then a fixed resistor 10k ohms. By voltage divider we can differentiate each key pressed value. Similarly it is done for other keypad character.

 

Sketch:
char* keypressed = 0;
int keyboardPin = A0;
int keyboardValue = 0;

void setup() {
 Serial.begin(9600);
}

void loop() {

keyboardValue = analogRead(A0);

while(keyboardValue<25) {
 // do nothing utnil key is pressed
 keyboardValue = analogRead(A0);
 delay(50); 
 }
 readkeyboard();
}
void readkeyboard(){
 keyboardValue = analogRead(keyboardPin);
 Serial.print("pin value");
 Serial.println(keyboardValue);
 if ((keyboardValue <599) && (keyboardValue> 583)){keypressed = "0";}
 if ((keyboardValue <124) && (keyboardValue> 106)){keypressed = "1";}
 if ((keyboardValue <160) && (keyboardValue> 150)){keypressed = "2";}
 if ((keyboardValue <150) && (keyboardValue> 137)){keypressed = "3";}
 if ((keyboardValue <210) && (keyboardValue> 183)){keypressed = "4";}
 if ((keyboardValue <314) && (keyboardValue> 303)){keypressed = "5";}
 if ((keyboardValue <300) && (keyboardValue> 288)){keypressed = "6";}
 if ((keyboardValue <276) && (keyboardValue> 250)){keypressed = "7";}
 if ((keyboardValue <555) && (keyboardValue> 537)){keypressed = "8";}
 if ((keyboardValue <505) && (keyboardValue> 480)){keypressed = "9";}
 if ((keyboardValue <290) && (keyboardValue> 264)){keypressed = "*";}
 if ((keyboardValue <526) && (keyboardValue> 513)){keypressed = "#";}
 if ((keyboardValue <68) && (keyboardValue> 58)){keypressed = "A";}
 if ((keyboardValue <86) && (keyboardValue> 70)){keypressed = "B";}
 if ((keyboardValue <93) && (keyboardValue> 85)){keypressed = "C";}
 if ((keyboardValue <106) && (keyboardValue> 92)){keypressed = "D";}

while (keyboardValue> 25) {
 delay (100);
 keyboardValue = analogRead((A0));
 }
 Serial.println(keypressed);
 delay(1000);
}

 

Now come to the programming part. Define keypressed is character. A0 pin will read the keyboard value. Define keyboardValue as an integer. In setup function serial.begin will start serial communication between Arduino and computer. In loop function keyboardvalue equal to analogread A0. Whatever value A0 will get it will assign to keyboard value. While loop will run until the condition inside parenthesis become false.  So we have set condition keyboardvalue is less than 25 then execute the code inside the curly bracket. It will again check the value of keyboardvalue and take a delay of 50 millisecond. This code execute over and over again until the keyboard value will be greater than 25. Serial.print will print pin value and serial.println will print keyboard value. This two line is added in sketch for determining the range of each key value in keyboard. As we are using single wire so our output for each key should be different therefore Arduino can recognize particular character is pressed. So we will find the range. Upload this sketch and pressed the key for example I am pressing 1 it is showing some pin value. It is not exact same for each time. So take its range where it is highest value and lowest value. Simultaneously edit the code for 1 and 2 and so on. After giving range for each key you can delete this two line if you want only character as an output.  Here we are using if condition. If keyboard value ranges between particular values is true then Keypressed is set to show particular character. This values may be different for you if you use different resistor then this. I have also check values with voltage divider formula and with serial monitor it was showing 5 to 10 values difference between them. Values may be different because of resistor physical accuracy. So check each value properly. Otherwise you will get error like you are pressing 1 you got 3. If you are getting this error. Correct the range in code for that particular character. If this is not working try to change the resistor in that particular row or column. Finally serial.println will print the key pressed value. Delay is for 1 second. If you reduce below one second you will see your value is printed more than one time. So one second is sufficient for this.  As you can see when I pressed the key it is showing exact character which I pressed.

 

LIST OF COMPONENT BUY ONLINE:

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

http://amzn.to/2wjd7p4 (Keypad 4×4)

http://amzn.to/2wxPmWz  (Breadboard)

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

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

 

KEEP LEARNING KEEP MAKING 🙂

 

How to control servo motor with your finger by flex sensor

Arduino Tutorial #18 How to control servo motor with your finger by using flex sensor

Posted Leave a commentPosted in Arduino

Hi guys in this tutorial we will learn how to control servo motor with your fingers by using Flex sensor. So let’s get started.

For this you will need

  1. Arduino,
  2. Servo motor,
  3. Flex sensor,
  4. Jumper wires,
  5. Breadboard and
  6. 56K ohms resistor.

Flex sensor used in many application such as robotics, gaming device, biometrics and fitness products. Flex sensor changes its resistance when it bends.

flex sesnor resistance changes
flex sesnor resistance changes

 

In normal flat position of sensor its resistance is 30k ohms and when it is bended its resistance increases up to 90k ohms resistance. This means that it won’t give 0 to 5V range. We will see it later in this video.  This range of resistance is depend upon flex sensor which you bought. Do connection as shown in Diagram.

 

Circuit Diagram:

 

servo with flex sensor circuit diagram
servo with flex sensor circuit diagram

 

Sketch:

 

#include <Servo.h>

Servo myservo;

int flexpin = A0; 
int val;

void setup() {
 myservo.attach(9); 
}

void loop() {
 val = analogRead(flexpin); 
 val = map(val, 370, 530, 0, 180); 
 myservo.write(val); 
 delay(10); 
}

 

We know that it won’t give us analog input range 0 to 1023. So we will determine the range of analog input. Go to file -> Example -> Basics -> AnalogReadSerial. Upload the sketch and go to tools and serial monitor. Now we can see the values when it is flat it is giving value around 370. And when it is bended it shows 530 value. Note this value as we need this value to map our servo angles. This value may be different for you if you are using other than 56k ohms resistor. You can calculate this range by voltage divider formula.

Vout=R2/(R1+R2)*Vin

voltage input = 5V
R1 = 56K ohms
R2 = 30K ohms (when flat)
90k ohms (when bended)
voltage output = 1.74V to 3.08V

Analog input = 1.74 * 1023/5 = 356
(for flat position)

Analog input = 3.08 * 1023/5 = 630
(for bended position)

I have taken 370 to 530 according to finger bending

You can see its values are almost same as what we got from serial monitor, it is showing 630 value for bending but we took 530 value for bending because when I bend my finger with sensor it won’t bend that much so I have taken values according to my fingers bending. Now let’s come to the programming part. #include will include outside library in our sketch. Create servo object to control a servo motor. Analog pin A0 is connected to flex sensor. Define variable for reading flex sensor value.  We have attached our servo to pin number 9. Analogread will read flex sensor value between 0 to 1023 and assign it to val. Now we want to map this value to servo angle. We know that it won’t range between 0 to 1023. We have previously checked it and it ranges between 370 to 530. So we will map it, for 370 it is 0 degree angle and for 530 it is 180 degree angle. Myservo.write will write flex sensor value and make angle rotation accordingly.  10 millisecond delay is just for servo to get there. You can see when I bend my finger it rotate anticlockwise and when I straight the finger it rotate clockwise.

 

LIST OF COMPONENT BUY ONLINE:

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

http://amzn.to/2weYm4R (Servo Motor)

http://amzn.to/2wfeuDx  (Flex sensor)

http://amzn.to/2wxPmWz  (Breadboard)

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

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

KEEP LEARNING KEEP MAKING 🙂

How to control brightness of LED with Potentiometer.

Arduino Tutorial # 17 How to control brightness of LED with Potentiometer.

Posted Leave a commentPosted in Arduino

Hi guys, in this tutorial we will see how to control brightness of LED with potentiometer. So let’s get started.

For this you will need

  1. Arduino,
  2. 3 pin Potentiometer,
  3. Breadboard,
  4. Jumper wire,
  5. LED,
  6. 1k ohms resistor.

A potentiometer is an electronic component in which we can adjust current manually by increasing or decreasing resistance. This is used in many application such as fan regulator, control brightness of light, volume control knob in music system.  Do connection as shown in diagram.

 

Circuit diagram:

 

Control brightness of LED with Potentiometer
Control brightness of LED with Potentiometer

 

int LED = 11;

void setup() {

pinMode (LED, OUTPUT);
 
}



void loop() {

int potentioValue = analogRead(A1);
 
int writevalue = (255.0/1023.0) * potentioValue;

analogWrite(LED, writevalue);

delay(1); 
}

 

Now let’s come to the programming part. Define LED pin number to 11. Make sure which ever you choose pin number it has to be PWM pin. In setup function pinmode of LED is output. In loop function we are defining integer potentioValue and assigning it to analogRead A1. Whatever A1 get input from potentiometer. Its value will be assign to potentioValue. In next line, here is also we are defining integer writevalue and assigning it to a formula for mapping analog input to PWM output.

 

Analog and PWM range
Analog and PWM range

 

Analog input ranges from 0 to 1023 and PWM output ranges from 0 to 255. So we should match this value in one scale. Therefore we have divided 255 with 1023 and multiply with potentionValue. Its maximum value will be 1023 so it means we get 255 as an output. If we don’t map this value what happen??

without mapping analog to PWM output
without mapping analog to PWM output

 

You can see its brightness increase up to 255 value then it is off for 256 value and again increase brightness repeatedly until potentiometer knob reaches at its end position. Analogwrite will take writevalue and make LED fade on or off. If we forget to take .0 after our formula (255/1023), LED will not On because it is giving zero value for any position of knob we can see on serial monitor. (int writevalue = 255.0/1023.0 * potentioValue) instead of this formula we can use map function, map(potentioValue, 0, 1023, 0, 255), parameter of map function are map(value, fromLow, fromHigh, toLow, toHigh) “value” which we want to map and we know low value for analog function is 0 and high is 1023 and we want to map it for PWM output. So Low value for PWM output is 0 and high value is 255. You can see when I rotate knob of potentiometer it control  brightness of LED. If you rotate knob in direction of 5v it will glow LED and opposite will make it fade off.

 

 

LIST OF COMPONENT BUY ONLINE:

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

http://amzn.to/2vYitnO (Potentiometer)

http://amzn.to/2wxPmWz  (Breadboard)

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

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

http://amzn.to/2vSpUON   (LED)

KEEP LEARNING KEEP MAKING 🙂