Working With Built-in Display and Push Buttons of the Norvi Device
by NORVI Controllers in Circuits > Microcontrollers
1501 Views, 4 Favorites, 0 Comments
Working With Built-in Display and Push Buttons of the Norvi Device
Introduction
Norvi devices are ESP32/ ESP8266/ STM32 embedded industrial controllers. These devices come with variety of features that make them ideal for industrial automation and IoT solutions.
In this instructable, we’ll be looking at how to work with the built-in display and push buttons of the Norvi devices. For this demonstration, we’ll be using the Norvi IIOT-AE01-R Device.(GPIOs will differ with different devices.)
So what do you need to get started?
- The Norvi IIOT-AE01-R Device
- USB cable(Type A to Type B mini)
- PC/Laptop
Understanding More About the Display
- The device has a built-in 0.96 OLED SSD1306 display of 128 x 64 resolution.
- The GPIOs 16(SDA) and 17(SCL) of ESP32 in the device are used for I2C communication with this display.
Understanding More About the Push Buttons.
- The device has 3 push buttons connected to a single analog input pin (GPIO 32) of ESP32.
- Here the buttons are separated by resistors internally, as shown above in the diagram. Hence with one analog input pin, the operation of 3 different buttons is achieved.
Programming the Device to Use the Display and the Push Buttons
-
Install the Adafruit SSD1306 and Adafruit GFX library in your Arduino IDE and upload the below sketch. (For installing ESP32 board and booting up the device, check the steps 1 and 2 of our previous instructable - Getting Started With Norvi Devices)
// set pin numbers const int buttonPin = 32; const int O1 = 14; const int O2 = 12; const int O3 = 13; const int O4 = 15; const int O5 = 2; const int O6 = 33; #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) int buttonState = 0; // variable for storing the pushbutton status int count = 0; //variable for storing the number of time the push button is pushed void setup() { Serial.begin(115200); pinMode(buttonPin, INPUT); // initialize the inputs and outputs pinMode(O1, OUTPUT); pinMode(O2, OUTPUT); pinMode(O3, OUTPUT); pinMode(O4, OUTPUT); pinMode(O5, OUTPUT); pinMode(O6, OUTPUT); Wire.begin(16, 17); // beginning the I2C connections display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //starting the display display.display(); delay(1000); display.clearDisplay(); } void loop() { buttonState = analogRead(buttonPin); // read the state of the pushbutton value Display_Update();// Updating the display as per the button count if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 0) { display.clearDisplay(); digitalWrite(O1, HIGH); // turn the output 1 on digitalWrite(O2, LOW); digitalWrite(O3, LOW); digitalWrite(O4, LOW); digitalWrite(O5, LOW); digitalWrite(O6, LOW); Display_Update();// Updating the display as per the button count buttonState = analogRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED ONCE"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 1) { // turn LED on display.clearDisplay(); digitalWrite(O1, LOW); digitalWrite(O2, HIGH); digitalWrite(O3, LOW); digitalWrite(O4, LOW); digitalWrite(O5, LOW); digitalWrite(O6, LOW); Display_Update();// Updating the display as per the button count buttonState = digitalRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED TWICE"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 2) { // turn LED on display.clearDisplay(); digitalWrite(O1, LOW); digitalWrite(O2, LOW); digitalWrite(O3, HIGH); digitalWrite(O4, LOW); digitalWrite(O5, LOW); digitalWrite(O6, LOW); Display_Update();// Updating the display as per the button count buttonState = digitalRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED THRICE"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 3) { // turn LED on display.clearDisplay(); digitalWrite(O1, LOW); digitalWrite(O2, LOW); digitalWrite(O3, LOW); digitalWrite(O4, HIGH); digitalWrite(O5, LOW); digitalWrite(O6, LOW); Display_Update();// Updating the display as per the button count buttonState = digitalRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED 4TH TIME"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 4) { // turn LED on display.clearDisplay(); digitalWrite(O1, LOW); digitalWrite(O2, LOW); digitalWrite(O3, LOW); digitalWrite(O4, LOW); digitalWrite(O5, HIGH); digitalWrite(O6, LOW); Display_Update(); buttonState = digitalRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED 5TH TIME"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 5) { // turn LED on display.clearDisplay(); digitalWrite(O1, LOW); digitalWrite(O2, LOW); digitalWrite(O3, LOW); digitalWrite(O4, LOW); digitalWrite(O5, LOW); digitalWrite(O6, HIGH); Display_Update();// Updating the display as per the button count buttonState = digitalRead(buttonPin); if (buttonState == 0) { Serial.println("BUTTON PRESSED 6TH TIME AND GONNA REPEAT AGAIN"); count = count + 1; } } if ((buttonState >= 3290 ) && (buttonState <= 3310) && count == 6) { count = 0; } } void Display_Update() { display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(45, 0); display.println("Outputs"); display.setCursor(25, 15); display.println(digitalRead(14)); display.setCursor(40, 15); display.println(digitalRead(12)); display.setCursor(55, 15); display.println(digitalRead(13)); display.setCursor(70, 15); display.println(digitalRead(15)); display.setCursor(85, 15); display.println(digitalRead(2)); display.setCursor(100, 15); display.println(digitalRead(33)); display.display(); delay(80); }
Understanding the Code
- In the following lines, you create variables to assign the GPIO pins for the button and the 6 relay outputs.
const int buttonPin = 32; int O1 = 14; int O2 = 12; int O3 = 13; int O4 = 15; int O5 = 2; int O6 = 33;
-
Next, the necessary libraries are imported to use I2C (Wire library) and to write the display. (Adafruit_SSD1306 and Adafruit_GFX libraries)
#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
- Then, the width and height of our OLED display are defined.
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64
-
Next, with the I2C communication protocol (&Wire), a display object with the width and height defined earlier is initialized.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
- In the below lines, variables are created to store the push-button status and to check the number of times the push button is being pressed.
int buttonState = 0; int count = 0;
-
Next, in setup(), we initialize the Serial Monitor at a baud rate of 115200.
Serial.begin(115200);
- Then using the pinMode() function, the button is defined as input and relay outputs as outputs.
pinMode(buttonPin, INPUT); pinMode(O1, OUTPUT); pinMode(O2, OUTPUT); pinMode(O3, OUTPUT); pinMode(O4, OUTPUT); pinMode(O5, OUTPUT); pinMode(O6, OUTPUT);
- In the following line, you initialize I2C by setting the sda and scl GPIOs to communicate with the display.
Wire.begin(16,17);
- Next, the display is initialized using its address.
display.begin(SSD1306_SWITCHCAPVCC,0x3C);
- Using the below lines we can ensure whether our display is working correctly. Here the display check is done for 1s (1000ms) and this can be varied.
display.display(); delay(1000); display.clearDisplay();
- In loop(), we start our code by reading the push button's state.
buttonState = analogRead(buttonPin);
- A function "Display_Update()" is created to display the relay output status in the OLED screen. Inside the function, text size, text color and text position are defined. This function is called every time when the button is pressed and the updated relay output status is displayed.
void Display_Update() { display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(45,0); display.println("Outputs"); display.setCursor(25,15); display.println(digitalRead(14)); display.setCursor(40,15); display.println(digitalRead(12)); display.setCursor(55,15); display.println(digitalRead(13)); display.setCursor(70,15); display.println(digitalRead(15)); display.setCursor(85,15); display.println(digitalRead(2)); display.setCursor(100,15); display.println(digitalRead(33)); display.display(); delay(80); }
- In the below lines, first the button state and the count value are checked. Then the previous screen display is cleared and accordingly the relay outputs are written as "HIGH" and "LOW". Then the function "Display_Update()" is called to display the updated relay output status. Also by using the Serial.println() function the button count is checked in the serial monitor in Arduino IDE. This is repeated until toggling is achieved.
if((buttonState >= 3290 ) && (buttonState <= 3310) && count == 0) { display.clearDisplay(); digitalWrite(O1, HIGH); // turn the output 1 on digitalWrite(O2, LOW); digitalWrite(O3, LOW); digitalWrite(O4, LOW); digitalWrite(O5, LOW); digitalWrite(O6, LOW); Display_Update();// Updating the display as per the button count buttonState = analogRead(buttonPin); if (buttonState == 0){ Serial.println("BUTTON PRESSED ONCE"); count=count+1; } }
Checking the Display
- Once you upload the sketch and boot up the device you can see that the device is displaying “Outputs” with the 6 respective relay output status. (starting from O1 - O6)
- Here we have selected the third push-button which gives an ADC range 3290-3310, for our demonstration. (This can be done using any push buttons and for this purpose, the ADC range has to be verified and changed accordingly in the code to make the desired button function.)
- Once the push button is pressed the first relay output O1 gets turned on and you can toggle the remaining outputs in order by pressing the button again.
- Apart from the display, you can confirm the toggling by looking at the LED indicators inside the device. Also by using the serial monitor in the Arduino IDE, you can verify the number of times the push button is being pressed.
To check more about the Norvi lineup - www.norvi.lk