Medium Basics Project 004e 28 BYJ - 48 Stepper Motor with LEDs, LCD1602 I2C, RFID RC522 and piezo buzzer

of Acoptex.com in UNO

Basics: Project 004e

Project name: Security system / 28 BYJ - 48 Stepper Motor with LEDs, LCD1602 I2C, RFID RC522 and piezo buzzer.

Attachments: libraries and program, pitches.h

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.BYJ 48 Stepper Motor

3.Arduino IDE ( you can download it from here  )

4.Jumper cables

5.ULN2003 stepper motor driver board

6. LEDs 2 pcs ( 1 Green, 1 Red)

7. Resistors 2 pcs (220 Om for each LED) + 1 pc (150 Om for piezo buzzer/optional/)

8. LCD1602 module

9. MFRC522 13.56Mhz IC or RFID RC522 module

10. Piezo buzzer

11. Breadboard

Understanding the stepper motors

See more details here. We will use unipolar stepper motor 28BYJ-48 for this project.

There are many types of drivers - L293, ULN2003, A3967SLB and more. 

The 28BYJ-48 even comes with breakout using ULN2003 as a motor driver chip.

Understanding LCD 1602 module

 See more details here - check this project

Understanding RFID RC522 module

 See more details here.

Understanding the piezo buzzer

Buzzers can be found in alarm devices, computers, timers and confirmation of user input such as a mouse click or keystroke.

Buzzers can be categorized as active buzzers and passive ones.

Place the pins of two buzzers face up and you can see the one with a green circuit board is a passive buzzer, while the other with a black tape, instead of a board, is an active buzzer, as shown below.

An active buzzer has a built-in oscillating source, so it will make sounds when electrified. But a passive buzzer does not have such source, so it will not beep if DC signals are used; instead, you need to use square waves whose frequency is between 2K and 5K to drive it. The active buzzer is often more expensive than the passive one because of multiple built-in oscillating circuits.

See piezo buzzers data sheets here.

Don’t forget to use a current-limiting resistor.

Signals and contacts stepper motor, ULN2003 module, LEDs, LCD 1602 module, RFID RC522 module 

Signals and connections of stepper motor, ULN2003 module, LEDs  you can find here.

Signals and connections of LCD1602 module you can find here.

Signals and connections of RFID RC522 module you can find here.


Signals and connections of active piezo buzzer
Active piezo buzzer pin marked with (+) sign should be connected to Arduino Uno board digital pin which sends a signal, the other pin of active piezo buzzer should be connected to Arduino Uno board GND.


Signals and connections of active piezo buzzer

Active piezo buzzer pin marked with (+) sign should be connected to Arduino Uno board digital pin which sends a signal, the other pin of active piezo buzzer should be connected to Arduino Uno board GND.


The following table and picture shows the needed connections with the Arduino Uno.

The following table and picture shows the needed connections with the Chinese Uno.

See the video here: https://youtu.be/eGL5ja8BONU

Step by Step instruction

  1. Plug your Adruino Uno board into your PC and select the correct board and com port
  2. Find your I2C address. Each device has an I2C address that it uses to  accept commands or send messages. Load the sketch over at http://arduino.cc/playground/Main/I2cScanner and follow the instructions to use it.  By opening up the Serial monitor window after you upload the sketch, Arduino will scan the address range looking for a reply.  Even though the documentation said it was 0x27, this scanner can detect different (in my case 0x3F)
  3. Modify the sketch in attachments above (you can use the sketch below too): the line LiquidCrystal_I2C lcd (0x 3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE) (See part marked bold)
  4. You must take something in consider: 28BYJ-48 has a gear ratio of 64 and stride angle 5.625 so this motor has a 4096 Steps. Number of Steps in one revolution= 360/stride angle=360/5.625=64. Steps =Number of steps in One Revolution * Gear ratio. Steps= (360/5.625)*64 = 64 * 64 =4096 - this value will substitute it in the arduino sketch. 
  5. For Adafruit Stepper Motor the stride angle 7.5 and gear ratio is 16. So number of steps in 1 revolution will be : steps in One Revolution  = 360 / 7.5 = 48. Steps= 48 * 16 = 768. It will be different depending on what motor you are using so check the datasheet for your stepper motor to calibrate these values.
  6. Open up serial monitor and set your baud to 9600 baud.
  7. Scan your card, you will see a bunch of numbers pop out.
  8. Copy only the number and paste it to the column after "int cards[][5] = { " - replace with your card number or numbers, space them with ',' for example my cards were: 0 64 134 25 223}. You can add many cards here. 
  9. Verify and upload the the sketch to your Adruino Uno.



  • See attachments on the begining of this project description.
  • pitches.h file contains all the pitch values for typical notes. For example, NOTE_C4 is middle C. NOTE_FS4 is F sharp, and so forth. This note table was originally written by Brett Hagman, on whose work the tone() command was based. You may find it useful for whenever you want to make musical notes.File should be in the same directory with sketch.
  • The Stepper, SPI and Wire library included in Adruino IDE program which installed in your PC.
  • You will need to unzip and add the RFID library to libraries in your PC, for example C:\Users\toshiba\Documents\Arduino\libraries
  • I have used the library - NewliquidCrystal_1.3.4.zip which I downloaded, unzipped, changed the name of folder to LiquidCristal and added to libraries, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.
  • If you have LiquidCristal folder in this location already - delete this folder and copy folder, which was made by you, to this location.


  name: BYJ48 Stepper motor code
  function: This program drives a unipolar or bipolar stepper motor and
  can be used for security system. LCD1602 module and 2 LEDs show the
  status of operation the piezo buzzer plays melody when the card accepted
  and rejected. The steps and motor speed can be adjusted easily
  to suit your needs. The molodies can be changed too.
// include the library code
#include <Stepper.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <RFID.h>
#include "pitches.h"
#define motorSteps 64     // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 4
#define motorPin2 2
#define motorPin3 3
#define motorPin4 9
#define ledPin1 6
#define ledPin2 7
#define SS_PIN 10
#define RST_PIN 5
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1, motorPin2, motorPin3, motorPin4);
// initialize of the RFID library:
// initialise the LCD1602 I2C:
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// Melodies definition: access and rejection - you can change melodies. see in file pitches.h
int access_melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int access_noteDurations[] = {4,8,8,4,4,4,4,4};
int noaccess_melody[] = {NOTE_G2, NOTE_G2};
int noaccess_noteDurations[] = {8,8};
int speaker_pin = 8;
// definition of cards:
int cardRead;  // card read 1 = good 0 = bad for playTune function
int power = 8;
int serNum[5];
int cards[][5] = {
  {0, 64, 134, 25, 223}, //Card 1
bool access = false;
void setup() {
  // set the motor speed at 200 RPMS:
  // initialize the Serial port:
  // initialize the SPI and RFID:
  // set up speaker pin:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  // define the LCD as 16 column by 2 rows
  lcd.begin (16, 2);
  // switch on the backlight
void loop() {
  //goto column 4 and first line
  lcd.setCursor(4, 0);
  // Print at cursor Location:
  //goto column 2 and second line
  lcd.setCursor(2, 1);
  if (rfid.isCard()) {
    if (rfid.readCardSerial()) {
      Serial.print(" ");
      Serial.print(" ");
      Serial.print(" ");
      Serial.print(" ");
      for (int x = 0; x < sizeof(cards); x++) {
        for (int i = 0; i < sizeof(rfid.serNum); i++ ) {
          if (rfid.serNum[i] != cards[x][i]) {
            access = false;
          } else {
            access = true;
        if (access) break;
    if (access) {
      // Opening the door:
      cardRead = 1;
      lcd.setCursor(4, 0);
      lcd.setCursor(2, 1);
      // Access LED - green - on:
      digitalWrite(ledPin1, HIGH);
      // Delay set to pass the door = 5000 miliseconds = 5 seconds:
      lcd.setCursor(2, 0);
      lcd.print("PASS THE DOOR");
      lcd.setCursor(2, 1);
      Serial.println("PASS THE DOOR");
      // Closing the door:
      lcd.setCursor(4, 0);
      lcd.setCursor(2, 1);
      // Access LED - green - off:
      digitalWrite(ledPin1, LOW);
      digitalWrite(power, LOW);
    } else {
      // No access - wrong card:
      cardRead = 0;
      Serial.println("NO ACCESS");
      lcd.setCursor(3, 0);
      lcd.print("NO ACCESS");
      lcd.setCursor(2, 1);
      // No access LED - red - on:
      digitalWrite(ledPin2, HIGH);
      // No access LED - red - off:
      digitalWrite(ledPin2, LOW);
      digitalWrite(power, LOW);
//Function to play the melody during Access and No access
void playTune(int Scan) {
  if (Scan == 1) // Card with Access scanned
    for (int i = 0; i < 8; i++)    //loop through the notes
        { // Good card read
          int access_noteDuration = 1000 / access_noteDurations[i];
          tone(speaker_pin, access_melody[i], access_noteDuration);
          int access_pauseBetweenNotes = access_noteDuration * 1.30;
    else // Card with No Access scanned
       for (int i = 0; i < 2; i++)    //loop through the notes 
          int noaccess_noteDuration = 1000 / noaccess_noteDurations[i];
          tone(speaker_pin, noaccess_melody[i], noaccess_noteDuration);
          int noaccess_pauseBetweenNotes = noaccess_noteDuration * 


Other projects of Acoptex.com
Easy Merry Xmas to everyone of Acoptex.com, Not selected 24-12-2019

Published at 03-05-2017
Viewed: 2961 times