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

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

1 Person Made This Project!

Recommendations

  • Mason Jar Speed Challenge

    Mason Jar Speed Challenge
  • Bikes Challenge

    Bikes Challenge
  • Remix Contest

    Remix Contest

3 Discussions

0
JhonDee
JhonDee

5 months ago

Modifique el código un poco, en el lugar de los puntos para configurar, agregó una barra de desplazamiento. Por lo demás, todo bien, el siguiente es mi código:
#include <LCD5110_Graph.h>
#define PIN_RESET 7 // LCD RST .... Pin 1
#define PIN_SCE 6 // LCD CS .... Pin 2
#define PIN_DC 5 // LCD Dat/Com. Pin 3
#define PIN_SDIN 4 // LCD DIN .... Pin 4
#define PIN_SCLK 3 // LCD SPIClk . Pin 5
int calibrationTime = 20; // Sensor PIR debe calibrarse, no haga mucho movimiento durante este tiempo
long unsigned int lowIn;
long unsigned int pause = 5000; //El tiempo que el sensor tiene que ser bajo antes de asumir que no hay movimiento
bool lockLow = true; //Variables por el hecho de que el sensor baja a veces y corregimos
bool takeLowTime;
int motionPin = 8; //Estamos conectados al pin 8
int ledPin = 13; //También vamos a mostrar si se detecta movimiento en el pin 13 (ALTO = Movimiento, BAJO = Sin movimiento)
LCD5110 lcd(3,4,5,7,6); //Hacer objeto lcd
// ****
#define LCD_C LOW
#define LCD_D HIGH
#define LCD_X 84
#define LCD_Y 48
#define LCD_CMD 0
int pixels[85][6] = {{0}};
void gotoXY(int x, int y)
{
LcdWrite( 0, 0x80 | x); // Column.80
LcdWrite( 0, 0x40 | y); // Row. 40
}
// Habilitar o deshabilitar un píxel específico
// x: 0 to 84, y: 0 to 48
void setPixel(int x, int y, int d) {
if (x > 84 || y > 48) { return; }
// La pantalla LCD tiene 6 filas, con 8 píxeles por fila.
// 'y_mod' is the row that the pixel is in.
// 'y_pix' is the pixel in that row we want to enable/disable
int y_mod = (int)(y >> 3); // >>3 divides by 8
int y_pix = (y-(y_mod << 3));// <<3 multiplies by 8
int val = 1 << y_pix;
// Tenemos que hacer un seguimiento de qué píxeles están activados/desactivados
// para escribir el carácter correcto en la pantalla LCD.
if (d){
pixels[x][y_mod] |= val;
} else {
pixels[x][y_mod] &= ~val;
}
// Escriba el píxel actualizado en la pantalla LCD
// TODO Comprueba si el píxel ya está en el estado solicitado,
// Si es así, no escriba en la pantalla LCD.
gotoXY(x,y_mod);
LcdWrite (1,pixels[x][y_mod]);
}
void LcdClear(void) {
for (int index = 0; index < LCD_X * LCD_Y / 8; index++) {
LcdWrite(LCD_D, 0x00);
}
}
// DibujA una línea horizontal de ancho w desde x, y
void drawHorizontalLine(int x, int y, int w){
drawHorizontalLineXY(x,x+w,y);
}
// Dibuja una línea horizontal entre x1 y x2 en la fila y
void drawHorizontalLineXY(int x1, int x2, int y){
for (int i=x1;i<=x2;i++) {
setPixel(i,y,1);
}
}
// Dibuja una line vertical de height h desde x,y
void drawVerticalLine(int x, int y, int h){
drawVerticalLineXY(y,y+h,x);
}
// Draw a vertical line from y1 to y2 on column x
void drawVerticalLineXY(int y1, int y2, int x){
for (int i=y1;i<=y2;i++)
{
setPixel(x,i,1);
}
}
void clearPixels() {
for (int x=0;x<83;x++) {
for(int y=0;y<47;y++) {
pixels[x][y] = 0;
}
}
}
// Draw a rectangle of width w and height h from x,y
void drawRect(int x, int y, int w, int h) {
drawHorizontalLineXY(x,x+w,y);
drawHorizontalLineXY(x,x+w,y+h);
drawVerticalLineXY(y,y+h,x);
drawVerticalLineXY(y,y+h,x+w);
}
void initProgressBar() {
LcdClear();
clearPixels();
drawRect(10,21, 64, 5);
}
void LcdWrite(byte dc, byte data)
{
digitalWrite(PIN_DC, dc);
digitalWrite(PIN_SCE, LOW);
shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
digitalWrite(PIN_SCE, HIGH);
}
// ****
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); //Configurando nuestra en una pequeña fuente
//Dando al sensor algo de tiempo para calibrar (como se dijo, haga un movimiento mínimo durante este tiempo)
Serial.println("Calibrating sensor"); //Permitir al usuario ahora a través del monitor serie que estamos calibrando
lcd.clrScr();
lcd.print("Calibrating", CENTER, 16); //Mismo trato
lcd.update();
for (int i = 0; i < calibrationTime; i++) {
Serial.print(".");
delay(1000);
}
Serial.println("Calibrado realizado");
Serial.println("El sensor ahora está activo y el código se está ejecutando");
lcd.clrScr();
lcd.print("Hecho", CENTER, 16);
lcd.update();
delay(100); //Pequeño retraso solo para asegurarse de que todo funcione sin problemas
lcd.clrScr();
lcd.print("Sin movimiento", CENTER, 14);
lcd.print("detectado", CENTER, 24);
lcd.update();
}
void loop() {
if (digitalRead(motionPin) == HIGH) { //Comprobando si hay movimiento o no
digitalWrite(ledPin, HIGH); //El led visualiza la salida de los sensores
if (lockLow) {
//se asegura de que esperemos una transición a LOW antes de que se realice cualquier salida adicional:
lockLow = false;
Serial.println("---");
Serial.println("Movimiento detectado");
lcd.clrScr();
lcd.print("Movimiento", CENTER, 14);
lcd.print("detectado", CENTER, 24);
lcd.update();
delay(20);
}
takeLowTime = true;
}
if (digitalRead(motionPin) == LOW) {
digitalWrite(ledPin, LOW);
if (takeLowTime) {
lowIn = millis(); //ahorrando el tiempo de la transición de ALTO a BAJO
takeLowTime = false; //asegurándose de que esto solo se haga al comienzo de la fase BAJA
}
//si el sensor está bajo por más de la pausa dada,
//asumimos que no ocurrirá más movimiento
if (!lockLow && millis() - lowIn > pause) {
lockLow = true;
lcd.clrScr();
lcd.print("Sin movimiento", CENTER, 14);
lcd.print("detectado", CENTER, 24);
lcd.update();
digitalWrite(ledPin, LOW);
Serial.println("movimiento finalizado");
delay(10);
}
}
}

0
Lacybad
Lacybad

3 years ago

I will make it in the weekend :)

0
kaffeetrinker71
kaffeetrinker71

3 years ago

Hi Samuel,

there is a little error in your code:
change
LCD5110 lcd(7, 4, 5, 3, 6); //Making lcd object
to
LCD5110 lcd(7, 4, 5, 2, 6); //Making lcd object