Maker Pro
Arduino

Keypad ESP32 OLED System

September 10, 2021 by Nick Khormaei
Share
banner

Keypad + Display system that shows user input and sends the number to a database

Hardware

Tools

1 SP15NUS

Software

1 Arduino IDE

The purpose of this project is to allow operators to enter in a number, then have that number sent over to a secure database as well as printed on the OLED Screen. This process is ideal for one or two systems, but if the process needs to be replicated I recommend getting a ribbon cable to connect the pins of the ESP32 to the pins of the keypad. 

Background/Prototype

On the main test machines, there is a computer installed that lets an operator control the settings. Along with that, it displays a number, called the R and D number, that keeps track of the test we are running for a specific machine. However, the non-main test machines (ovens, drill presses, etc) assist with the testing process but do not have a computer displaying or a way of tracking the corresponding R and D number. This system will allow an operator to enter a 5-digit R and D number, have it displayed on the screen and have that number sent over the network to the database. 

Each pin on the keypad corresponded to a specific row or column. Then it was connected to the GPIO pins of the ESP32 so the microcontroller can accept user input. Then, the ESP32 sends the data over to the OLED screen through I2C and SPI connections. It also powers the OLED screen with 3V and GND connections.  Also, there is an added "clear screen feature" so when the 'A' key is pressed the screen is cleared allowing for additional user input. 

MicrosoftTeams-image (3).png

Prototype of the system

/* Program used to take user input from 4x4 matrix keypad and output it to OLED using ESP32 board */
/* Also, sends the number to the database */ 
#include "Keypad.h"
#include "Arduino.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "WiFi.h"

const char* ssid       = "Ball";
const char* password   = "Nick";
const char* broker_ip = "10.111.12.1";

/* instatiate wifi, mqtt clients */
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);

/* Setting up the keypad */
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

/* For ESP32 Microcontroller */
byte rowPins[ROWS] = {15, 32, 14, 21}; 
byte colPins[COLS] = {13, 12, 27, 33};

Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // instantiating a keypad object 

/* callback function for mqtt connection- called when a new message is received
 * for a topic that we're subscribed to.*/
void mqtt_cb(char* topic, uint8_t *msg, uint32_t len){

}

/* handle a reconnect to the broker if the connection is dropped */
void mqtt_reconnect(void){
    while(!mqtt_client.connected()){
        Serial.printf("Connecting to broker '%s'...", broker_ip);
        if(mqtt_client.connect("R&D #")){
            Serial.println("CONNECTED");
        }
        else{
            Serial.println("FAILED:");
            Serial.print(mqtt_client.state());
            Serial.println("\r\nRetrying in 5 seconds.");
            delay(5000);
        }
    }

}



void setup() {
  Serial.begin(9600);
  
  Serial.println("OLED FeatherWing test");
  /* SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally */
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32

  Serial.println("OLED begun");

  /* Show image buffer on the display hardware. */
  /* Since the buffer is intialized with an Adafruit splashscreen */
  /* internally, this will display the splashscreen. */
  display.display();
  delay(1000);

  /* Clear the buffer. */
  display.clearDisplay();
  display.display();

  Serial.println("IO test");

  /* text display tests */
  display.setTextSize(4);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.display(); // actually display all of the above


// connect to WiFi
  Serial.printf("Connecting to '%s'...", ssid);

  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println(" CONNECTED");

  /* initialize (but don't connect yet) to the MQTT broker */
  mqtt_client.setServer(broker_ip, 1883);
  mqtt_client.setCallback(mqtt_cb);

}

void loop() {
  char key = keypad.getKey();

  if(!mqtt_client.connected()){
        mqtt_reconnect();
  }

   /* handle/maintain the MQTT connection, including any new messages */
  mqtt_client.loop();

  if (key == 'A'){
    display.clearDisplay();
    display.setCursor(0,0);
    display.display();
    
  } else if (key) {
    Serial.println(key);
    display.print(key);
    display.display();

    char msg[4]; // stores R&D # in an string
    snprintf(msg, 4, "%d", key);
    mqtt_client.publish("R & D Number: ", msg); // sends it to database
    
  }
}
Keypad Setup.PNG

Electrical Diagram of Prototype

MicrosoftTeams-image (7).png

System on protoboard

Capture.PNG

3D model of enclosure for the board

Related Content

Comments


You May Also Like