Introduction: Arduino LCD (Nokia 5110) and Motion Sensor (HC-SR501)

About: Hello, My name is Samuel, I am a student. I love micro controllers like arduino, they are my favorite interest. I also do geocaching, a worldwide treasure hunt game. I hope you enjoy my instructables! Samuel

Hello,

Welcome to this tutorial.

So If you are like me, and got a HC-SR501 and a nokia 5110 LCD on the mail the same day, you are probably gonna want to make something cool and/or test your new units. This tutorial will show you how to connect your HC-SR501 pir sensor and your Nokia 5110 in combination to create a cool little "interface". I guess you don't want me to rabble on here so we will start, first a little theory.

Step 1: How the Units Work

The 5110 is pretty self-explanatory on how it works, you basically plug it in, write some code and the display will turn pixels on or off reading your code.

The HC-SR501 on the other hand, is not so self-explanatory. It basically detects infrared light. Infrared light is emitted by everything moving, me, you, even a cat or a dog. When the sensor receives some infrared light (movement, it emits a high signal, and if there is no infrared light (once again, movement) it basically emits a low signal.

Let's now get our parts ready to build this project.

Step 2: Parts

Of course, you could create this project with any arduino, I am just using the UNO to make it a little easier to understand as most arduino-users start of with this board. As this tutorial is mostly made to show how the Nokia 5110 LCD can be used and how the HC-SR501 works, I will of course be using these, but you can use any LCD you want, but you'll have to plug in all your connections, edit your code and all that for yourself, which might get complicated in the end. You could also do this without a breadboard, but it is of course very useful for prototyping. Either way, here are the parts I will be using:

Arduino UNO (3-22$)

Breadboard (1-3$)

Some jumper cables (1-4$)

A Nokia 5110 (2$)

A HC-SR501 (1$)

Please contact me or comment if you have any question regarding where and how I got the products. Let's continue with wiring the LCD up.

Step 3: Connections (LCD)

As you see at the pictures above, the display runs on 8 pins:

GND, pretty self-explanatory

LIGHT, when this is plugged directly into GND the backlight is on, else if it is connected to VCC or nothing, the backlight will be off.

VCC, goes to arduino power. Remember the display runs on 3.3V and if you do connect it to 5V it might burn out.

CLK, pin for clock signal

DIN (MOSI), pin for data transfer

DC, pin for register select. Pretty much if you wanna send commands or data to the LCD.

CE aka CS, pin for chip select/enable.

RST, pin for reset

Now that you know this, let's connect everything up. Connect it according to the pictures or/and according to the below info:

GND -> GND

LIGHT -> GND (if you want backlight, otherwise VCC)

VCC -> Arduino 3.3V

CLK -> Arduino pin 7

DIN -> Arduino pin 4

DC ->Arduino pin 5

CE -> Arduino pin 6

RST -> Arduino pin 2

Now you can connect these to any pins, but you will need to make the changes in the code which I will not go through in this tutorial. Now, wiring of the sensor we are going to be using:

Step 4: Connections (sensor)

The sensor is fairly simple to connect, basically, GND, VCC and a digital output pin. Same deal, use the pictures and/or the info below to connect the sensor:

Connect the jumper cables to the sensor then connect,

GND -> Arduino GND

VCC -> Arduino 5V (I just used 5 volts because I think this sensor works better with 5V)

Digital output -> Arduino pin 8

That's it, fairly simple, eh? Now let me show you the code, which is not so complicated:

Step 5: Code

Code, the funniest part :) The code is fairly simple, but you need some knowledge to understand it (if that is your goal, to understand the code), I have tried to comment most of the code to make it easier for you to understand, but I do not know whether I did a good job or not. You will also need to download the LCD_5110_Graph library. Many thanks to Henning Karlsen for creating this!

Here is the code, enjoy:

#include <LCD5110_Graph.h>

int calibrationTime = 20; // PIR sensor has to be calibrated, do not make much movement during this time long unsigned int lowIn; long unsigned int pause = 5000; //The time the sensor has to be low before we assume there is no motion bool lockLow = true; //Variables for the fact that the sensor goes low sometimes and we correct for that bool takeLowTime;

int motionPin = 8; //We're connected to pin 8 int ledPin = 13; //We're also going to show if motion is detected on pin 13 (HIGH = Motion, LOW = No motion)

LCD5110 lcd(7, 4, 5, 3, 6); //Making lcd object

extern unsigned char SmallFont[]; // Including our smallfont (which is included with the library)

void setup() { Serial.begin(9600); //Starting serial communication lcd.InitLCD(); //Initiating LCD lcd.setFont(SmallFont); //Setting our font to a small one

//Giving the sensor some time to calibrate (As said, make minimal movement during this time)

Serial.println("Calibrating sensor"); //Letting user now via Serial monitor that we are calibrating lcd.clrScr(); lcd.print("Calibrating", CENTER, 16); //Same deal lcd.update(); for (int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println("Calibration done"); Serial.println("Sensor is now active and code is running"); lcd.clrScr(); lcd.print("Done", CENTER, 16); lcd.update(); delay(100); //Small delay just to make sure everything is running smooth lcd.clrScr(); lcd.print("No motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); }

void loop() {

if (digitalRead(motionPin) == HIGH) { //Checking whether there is motion or not digitalWrite(ledPin, HIGH); //The led visualizes the sensors output if (lockLow) { //makes sure we wait for a transition to LOW beforte any further output is made: lockLow = false; Serial.println("---"); Serial.println("Motion detected"); lcd.clrScr(); lcd.print("Motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); delay(20); } takeLowTime = true; } if (digitalRead(motionPin) == LOW) { digitalWrite(ledPin, LOW); if (takeLowTime) {

lowIn = millis(); //saving the time of the transition from HIGH to LOW takeLowTime = false; //making sure this is only done at the start of the LOW phase

} //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if (!lockLow && millis() - lowIn > pause) { lockLow = true; lcd.clrScr(); lcd.print("No motion", CENTER, 14); lcd.print("detected", CENTER, 24); lcd.update(); digitalWrite(ledPin, LOW); Serial.println("motion ended"); delay(10);

}

}

}

Step 6: Finish Line

Now if you done everything correctly, look at the pictures above, does it match? If yes, great and I hope you enjoyed the tutorial. If no, please do not hesitate to contact me or post a comment, I will get back to you.

Thanks for reading and have a great day!

Samuel