ESP32 - DIGITAL INPUT AND OUTPUTS



MOHAMMAD SAIFIQUL AIMAN B MOHAMMAD ALI

192011145

R2427-MICROELECTRONIC ENGINEERING

We'll teach you how to read digital inputs like a button switch and operate digital outputs like an LED in this section. If you've ever used the Arduino IDE to programme an Arduino or an ESP8266, you'll be familiar with this.

Here’s a list of parts you need to assemble the circuit

 ESP32 DOIT DEVKIT V1 Board

 5mm LED 

330 Ohm resistor 

Pushbutton 

10k Ohm resistor 

Breadboard 

Jumper wires


 digitalWrite() 

To control a digital output you just need to use the digitalWrite() function, that accepts as arguments, the GPIO you are referring to, and the state, either HIGH or LOW.

digitalRead() 

To read a digital input, like a button, you use the digitalRead() function, that accepts as argument, the GPIO you are referring to.



Assemble the circuit shown below



Copy the source code below

Source code :

// set pin numbers

 const int buttonPin = 4; // the number of the pushbutton pin

 const int ledPin = 16; // 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);

  }

 }



Uploading the Sketch :

Before clicking the upload button, go to Tools  Board, and select the board you’re using. In my case. It’s the DOIT ESP32 DEVKIT V1 board. Also don’t forget to select your ESP32’s COM port.



Now, press the upload button.

Then, wait for the “Done uploading.” message: 


Testing your project :




When pressing the button, LED will blink.









Comments

Popular posts from this blog

ESP32 Hall Effect Sensor

MINI PROJECT - AUTOMATIC WATERING PLANT USING BLYNK

ESP32 TOUCH SENSOR