Part 1 Home automation

Aim: To create a small scale home automation control, using a keypad code entry for which you can then do tasks such a lights and fan and read temperature on to a screen.

1) Taking basic steps i set-up and coded a keypad to turn on and off leds.

photo (10)

#include <Keypad.h>                                                              Keypad library, code and Event left and below

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘*’,’0′,’#’}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {12, 11, 10}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

In the set-up i needed to add an event:

keypad.addEventListener(keypadEvent); //add an event listener for this keypad

And the event to have the leds turn on using individual buttons and all off one button is:

void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
switch (key){
case ‘1’: digitalWrite(ledPin,!digitalRead(ledPin)); break;
case ‘2’: digitalWrite(ledPin2, !digitalRead(ledPin2); break;
case ‘3’: digitalWrite(ledPin3, !digitalRead(ledPin3); break;
case ‘4’: digitalWrite(ledPin4, !digitalRead(ledPin4); break;

case ‘*’:
digitalWrite(ledPin,!digitalRead(ledPin));
digitalWrite(ledPin2,!digitalRead(ledPin2));
digitalWrite(ledPin3,!digitalRead(ledPin3));
digitalWrite(ledPin4,!digitalRead(ledPin4));

break;

 

2) Next step was to add in a screen to read the key presses on:

Due to the aim of the project with the keypad and fan etc i couldn’t use a normal LCD screen. Instead i had to use a serial LCD. This means it only has 4 connections saving space on my Arduino breadboard. Two of these connections connected to two analogue pins.

photo 2 (13)photo (10)

The photos above show the LCD screen connected with only 4 wires. The other photo shows the screen reading the key pressed.

Serial LCD library:

#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystals_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <Wire.h>

/*—–( Declare Constants )—–*/
/*—–( Declare objects )—–*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

Welcome message when plugged in:

When the Arduino is plugged in i wanted the LCD to display a welcome message of Hello Jordon. In void setup this is the code i used to do that showing only the code related to the screen:

Void setup() {

lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight

Serial.begin(9600);

lcd.backlight(); // finish with backlight on

lcd.print(“Hello Jordon“);
delay(3000);
lcd.clear();

}

Reading the keypad numbers pressed on screen:

Below shows my code for the final piece. as a number is pressed it reads on screen then clears. in case of 1, Auto fan is printed and in case of 5, the temperature is printed.

In void loop:

char key = keypad.getKey();

int tempVal = (analogRead(tempPin)+40)/10;
if (blink){
lcd.clear();
lcd.write(“Auto fan”);
if(tempVal > 18)
digitalWrite(fan,HIGH);
else
digitalWrite(fan,LOW);   (this is for when the number 1 button is pressed Auto fan mode comes on, so as the temperature gets above 18 degrees the fan will come on automatically.)

In Keypad event:

//take care of some special events
void keypadEvent(KeypadEvent key){
switch (keypad.getState()){
case PRESSED:
lcd.clear();
lcd.print(key);
delay(500);
lcd.clear();
int tempVal2 = analogRead(tempPin);
int tempVal3 = ((tempVal2)+40)/10;
switch (key){
case ‘1’: blink = !blink; break;
case ‘#’: blink = false; digitalWrite(fan, !digitalRead(fan)); break;
case’5′: lcd.print(“Temperature: “); lcd.print(tempVal3); lcd.print(” degrees C”); delay(1000); break;

case’*’: blink = false; digitalWrite(ledPin, !digitalRead(ledPin));
break;
}
}
}

3) The Temperature sensor:

The temprature sensor is pretty simple to add in.

it has to be connected up to an analog pin: int tempPin= A1;

It has to be defined as an input in void setup: pinMode(tempPin,INPUT);

In the loop to read the pin: int tempVal = (analogRead(tempPin)+40)/10;

As shown above, this is the code for the Fan to automatically come on above 18 degrees.

if(tempVal > 18)
digitalWrite(fan,HIGH);
else
digitalWrite(fan,LOW);

Above shown previously about the LCD also shows the temp code in keypad event.

Connection of the temp sensor will be shown in part 2 on the breadboard layout diagram.

4) Next output was the Fan:

Above explains the Auto fan mode multiple times so not necessary to explain here.

The fan though can be turned on and off when desired using the # key. this will over ride the Auto fan mode if on.

transistor As i was using a 12v fan, i had to use an n channel Mosfet transistor which a photo can be seen above.

Again breadboard layout will come in part 2 to see how this is connected.

 5) Code entry to Arduino:

Unfortunately i couldn’t get the code i had working after spending many hours working out why. it kept resetting to a point before the void setup which is unusual. So in the end i had to not use a code entry and it will be something i will try to work out after the project.

i could get the outputs such as led and fan working before i typed in the code but not after.

i had a welcome message when the Arduino was plugged in and an enter code message.

Code:

#include <Keypad.h>
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystals_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <Wire.h>
/*—–( Declare Constants )—–*/
/*—–( Declare objects )—–*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
boolean unlocked = false;
int attemptsLeft = 3;
int userPosition = 0;
int ledState = 0;
int fanState = 0;
boolean ledOn = false;
boolean fanOn = false;
char code[] = {‘0′,’0′,’0′,’0’};
char userInput[4];
char keys[ROWS][COLS] = {
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘*’,’0′,’#’}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 10;
byte fanPin= 11;
boolean blink = false;
void setup(){
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
Serial.begin(9600);
lcd.backlight(); // finish with backlight on
pinMode(ledPin, OUTPUT); // sets the digital pin as output
// digitalWrite(ledPin, LOW); // sets the LED on
pinMode(fanPin,OUTPUT);
//digitalWrite(fanPin,LOW);
// keypad.addEventListener(keypadEvent); //add an event listener for this keypad
lcd.print(“Made by Jordon Trent”);
delay(2000);
lcd.clear();
}
void loop(){
lcd.clear();
lcd.print(“Enter code”);
delay(1000);
lcd.clear();
while(true){
char key = keypad.getKey();
if (key) {
if (unlocked){
switch (key){
case ‘*’: digitalWrite(ledPin,!digitalRead(ledPin)); break;
case ‘#’: digitalWrite(fanPin,!digitalRead(fanPin)); break;
break;
}
}
else{
userInput[userPosition] = key;
lcd.write(key);
if (userPosition++ == 4){
userPosition = 0;
int valid = true;
for (int i = 0; i<=3; i++){
if (userInput[i] != code[i])
valid = false;
}
if (valid == false){
if (attemptsLeft– == 0){
attemptsLeft = 3;
lcd.clear();
lcd.print(“Incorrect, wait 10 seconds”);
delay(1000);
lcd.clear();
break;
}
else {
lcd.clear();
lcd.print(“Incorrect, “+ String(attemptsLeft) + ” attempts left”);
delay(2000);
lcd.clear();
}
}
else{
lcd.clear();
lcd.write(“Correct!”);
delay(1000);
lcd.print(“Press # for fan and * for light”);
delay(1000);
lcd.clear();
unlocked = true;
break;
}
}
}
}
}
}

Part 2 shows the end result of the project with videos and final code.

Throughout this project i got inspiration from places like instructables and adafruit.

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s