Project #02: The Ins and Outs
“This machine needs a way to see and speak!”
Welcome to Project #2: The Ins and Outs. Here we’ll bring a new component for our project: "Buttons”. Before we start, let’s learn a bit about inputs and outputs! (๑˃ᴗ˂)ﻭ
Inputs are devices that will allow data to enter the program. In this case, we use a button as an input because it sends the data on whether the button was pushed or not, that data will be used in our code to determine what output ESP32 should produce.
Outputs are devices that will act as a result of our program. Here, we will use LED lamps as our output devices. The action that they will take is turning on or off based on the button’s input.
Part #1 — Coding
//set pin number
const int buttonPin = 4;
const int ledPin = 5;
int buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
As we’ve talked about in the last project, the code for ESP32 is split into two parts, setup and loop. We can split up the codes and examine how each of them functions:
//set pin number
const int buttonPin = 4;
const int ledPin = 5;
int buttonState = 0;
We assign which Pin is the buttonPin and which one is the ledPin. If we observe the ESP32 that we set up we can see that a jumper cable connects D4 with the button and another jumper cable connect D5 and the LED bulb. buttonState = 0 sets the initial state of the button to 0, meaning that initially, the button is not pressed.
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
This part of the code runs once when we turned on the device. It sets the buttonPin as an Input and the ledPin as an Output. This makes sense considering when the button is pressed as an input, the output is the LED turning on.
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
Lastly, the loop part of the code. It first reads whether or not our button is pushed. The state of the
Part #2 — Physical Components
The components you’ll need for this project are :
- ESP32
- MicroUSB cable
- 10 kΩ Resistor
- 330 Ω Resistor
- Button
- 1 LED light
- 5 Male-Male jumper cables
Firstly, you’ll need to set up your components using the following circuit :
Now, there are a few details that cannot be shown in the circuit and those are :
- The resistor that is connected to the LED light is a 330 Ω resistor
- The resistor that is connected to the button is a 10 kΩ resistor
- The longer LED pins connect to the jumper cable, meanwhile, the shorter one connects to the resistor
After you follow everything through, try uploading the code into your ESP32. After finished uploading it, the LED lamp should be turned off and turned on when you press the button.
After that, your project is finished! But again, I encourage you to explore new ideas and make your own creation from this simple project. Add more components, inputs, outputs, whatever you want it to be.
— Go and Explore! \(^▽^)/
Bonus Part — Experiments
Experiment 1: Siren
With this, you don't need to change a lot of aspects of the physical circuit you already have. You only need one more male-male jumper cable and a 330k resistor. You need to attach one end to the D21 pin and the other to the longer part of the LED. Here is the used code for this experiment:
//set pin number
const int buttonPin = 4;
const int ledPin1 = 5;
const int ledPin2 = 21;
int buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
delay(100);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(100);
}
In this experiment, we made a few modifications to the previous code :
const int buttonPin = 4;
const int ledPin1 = 5;
const int ledPin2 = 21;
this code is meant to set 2 output pins, ledPin1, and ledPin2 respectively. LedPin1 is assigned to the number 5 because there’s a jumper cable that connects the LED to the D5 pin. With this logic, we can give another output Pin to make it possible for us to use a second lamp in our project.
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
delay(100);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
delay(100);
}
In this tode, we turn off the other LED when one LED is on. we can set it manually and give it a 100-millisecond of delay. With this code, it’ll run in a loop and make our current project flashes two different colored LED in a loop. Making a Siren!
Experiment 2: Buzzer
This experiment has the exact same physical component as the making of the “Siren” experiment. Here is the used code for this experiment:
//set pin number
const int buttonPin = 4;
const int ledPin1 = 5;
const int ledPin2 = 21;
int buttonState = 0;
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == HIGH){
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
Serial.write("Button is Pressed!");
}
else{
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
}
}
with this code, we also put a few modifications to the previous code.
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
if (buttonState == HIGH){
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin1, LOW);
Serial.write("Button is Pressed!");
}
else{
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, HIGH);
}
}
with this if-else statement, we can see that if the button is pressed, the program will set ledPin2 to HIGH and ledPin1 to LOW and give us a message on the serial monitor that the button has been pressed. If the button is not pressed, a different LED with turn on. We can see the result of this experiment below :
— That’s all for this week folks!