Simple I/O : How to using GPIO on ESP32 as digital input and output. [FIND A ERROR]

Hello, future engineers!!

Welcome again to my blog!

My name is Muhamad Fariz Ramadhan from STI 2020 ITB. I made this blog to help you guys out about embedded system especially setting up ESP32 for getting some conditions. At this time, I am going to show you how using GPIO on ESP32 as digital input and digital output with Arduino UNO

*DON'T YOU GUYS KNOW??*
The combinations of GPIO can make a beautiful & unique circuit of system, here some examples

source : https://cdn.tindiemedia.com/

source :https://hackaday.com/

source: https://hackster.imgix.net/

    Very beautiful isn't it? So, now we're going to make it but in simple way first. We will only use a blink LED feature on an ESP32 dev kit using the Arduino IDE plus the external LED blink. Here is the tutorials.

STEP 1 : Required Hardware


1. ESP32 Development Board
2. Laptop / PC
3. Micro USB cable
4. 5 mm LED
5. Push-button
6. 330 Ohm resistor
7. 10k Ohm resistor
8. Jumper wires
9. Motherboard

*Urgent*
Make sure that you use micro USB cable that work properly because it's so sensitive, you can use your cable phone charger or buy the new one.


STEP 2 : Required Software

{SKIP IF YOU ALREADY INSTALLED ARDUINO IDE}

1. Download and install Arduino IDE


You can download Arduino IDE from this link: https://www.arduino.cc/en/software and choose suitable version for your laptop / PC. After that, please install the app by following the instructions.

2. Open Arduino IDE and set up for ESP32 microcontroller
After Arduino get installed then we have to set up the app to suitable for ESP32 microcontroller.

a.  Add boards manager URL by go to File > Preferences then add this link: https://dl.espressif.com/dl/package_esp32_index.json to Additional Boards Manager URLs and click OK.


b. Now, we're going to install the ESP32 board package. Go to Tools > Boards > Boards Manager. Then, search ESP32 by Espressif Systems and install it.

Now, change the board to DOIT ESP32 DEVKIT V1.


c. Install a USB to UART Port Driver.. One of the alternatives, you can get the driver from this link: https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers. Choose the CP210x VCP type.
For windows user you will get the zip folder that consist of these files. 
then install the execution file until the completed and the display looks like this.


STEP 3 : Circuit time & Demonstration

1. Learn the concept
Basically, the push button and LED is the key for this tutorial that will connect and link into the GPIO. Here are the illustrations.


source : randomnerdtutorials


2. Set up the circuit
From the illustration above, we can state that the situation of the circuit is:
  • 3V3 connect to positive side of motherboard
  • GND connect to negative side
  • The first pin of push-button connect to positive side, while other pin of push-button connect to GPIO and resistor 10k ohm that linked to negative side of motherboard
  • The positive pin of LED connect to GPIO, while the positive pin of LED connect to resistor 330 ohm that linked to negative side of motherboard
If you make it, then the circuit would be look like this.



3. Upload the code

Here is the code that:

// set pin numbers
const int buttonPin = 4;  // the number of the pushbutton pin
const int ledPin =  5;    // the number of the LED pin

// variable for storing the pushbutton status 
int buttonState = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off
    digitalWrite(ledPin, LOW);
  }
}

4. Demonstrations

When your code already uploaded, then test your circuit. The LED should light up when you only press the push-button:


Here is the video demonstration:



*FIND ERROR*
On my first attempt, the LED didn't lights up well (It was like bipping so fast uncontrolled) when the push-button get pressed. Here the video


After observation on the internet and listen explanation from Mr. Soni (my teacher) the problem is that on motherboard there is gap between left side area and the right one, only for positive and negative track. Here for the illustrations.



So, in conclusion if we want to use the area of left and right together, don't forget to linked it between them, like this.


Bonus : 

EXPLORE : Let's Make The Beautiful One!!

At this bonus section, I am going to show you some variations of digital inputs and outputs of ESP32 that more beautiful than the first one. 

1. Circuit with 2 push-buttons and 2 LED

First, let's make it simple. In this variate, we only add a new one push buttons and LED.
1. Set up the circuit exactly to the first tutorial above (non-bonus section).

2. Add a new LED and push button to the motherboard. Then, link it through the same way like the first one. In here I'm using GPIO 22 to connect the ledPin and GPIO 23 to connect pushButton Pin.

Copy this code:

//LED HIJAU
const int buttonPin2 = 4;  
const int ledPinGreen =  5;  
//LED KUNING
const int buttonPin = 22;
const int ledPinYellow = 23;  

// variable for storing the pushbutton status 
int buttonState = 0;
int buttonState2 = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
  // initialize the LED pin as an output
  pinMode(ledPinBlue, OUTPUT);
  pinMode(ledPinRed, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPinBlue, HIGH);
  } else if (buttonState == LOW) {
    // turn LED off
    digitalWrite(ledPinBlue, LOW);
  }
  Serial.println(buttonState2);
  if (buttonState2 == HIGH) {
    // turn LED on
    digitalWrite(ledPinRed, HIGH);
  } else if (buttonState2 == LOW) {
    // turn LED off
    digitalWrite(ledPinRed, LOW);
  }
}

How does it works?
  • If the first push-button is pressed and second push-button is not pressed, LED 1 will light up, while LED 2 will remain off. (1)
  • If the second push button is pressed and push button 1 is not, then LED 2 will light up and LED 1 will remain off. (2)
  • If both push buttons are pressed, both LEDs will light up. (3)
(1)

(2)

(3)


Here is the demonstration video:


2. Circuit with a push-button and 4 LED's

Second, let's make it more advance. In this variate, we add 3 more LED.
1. Set up the circuit exactly to the first tutorial above.

2. Add 3 more LED's the same way like the first tutorial, GPIO linked in to positive pin of LED and resistor linked in to the negative one. In here, I'm using GPIO 21, 22, 23 to connect the LED.

3. From this one circuit we can manipulate 2 kind of LED output. 

a. 2 LED on and 2 LED off
Copy this code:

// set pin numbers
const int buttonPin = 4;  

//LED HIJAU
const int ledPin1 =  5;    
const int ledPin2 =  21;
//LED KUNING
const int ledPin3 = 22;
const int ledPin4 = 23;

// variable for storing the pushbutton status 
int buttonState = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  } else {
    // turn LED off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
  }
}

How does it works?
  • If the push-button is pressed, all the Green LED will light up, while yellow LED  will remain off. (1)
  • If the push-button is unpressed, all the Green LED will remain off, while yellow LED  will light up. (2)
(1)


(2)

Here is the demonstration video.


b. Light up sequentially
Copy this code:

// set pin numbers
const int buttonPin = 4; 

//LED HIJAU
const int ledPin1 =  5;    
const int ledPin2 =  21;
//LED KUNING
const int ledPin3 = 22;
const int ledPin4 = 23;

// variable for storing the pushbutton status 
int buttonState = 0;

void setup() {
  Serial.begin(115200);  
  // initialize the pushbutton pin as an input
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

void loop() {
  // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(ledPin1, HIGH);
    delay(500);
    digitalWrite(ledPin1, LOW);
    delay(500);
    digitalWrite(ledPin3, HIGH);
    delay(500);
    digitalWrite(ledPin3, LOW);
    delay(500);
    digitalWrite(ledPin2, HIGH);
    delay(500);
    digitalWrite(ledPin2, LOW);
    delay(500);
    digitalWrite(ledPin4, HIGH);
    delay(500);
    digitalWrite(ledPin4, LOW);
    delay(500);
  } else {
    // turn LED off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
}

How does it works?
  • If the push-button is pressed, the LED sequentially lights up from left. (1)
  • By manipulate the code we can also lights up the LED sequentially from right (2)
  • Or even from the middle. (3)
(1)

(2)

(3)


That's it. Thank you guys.
Semoga bermanfaat!!
- Fariz STI'20 

Komentar

Postingan populer dari blog ini

ESP32 with Multiple I2C Devices

ESP32 Web Server : Turn on LED using WiFi from Your Mobile or Desktop

Displayed cute picture of cat with OLED Display on ESP32 + PWM