Digital input: Here instead of turning something on or off we are checking to see if something external to the microprocessor is turned on or off. This time the pin is going to "detect" the presence or absence of voltages. The voltages we check for should be high (around the same as the chips' supply voltage) or low (at or near ground or zero volts). The command is digitalRead but this time we need to set some other variable equal to the state of the input. First we need to set the pin to input mode use pinMode(pin,INPUT). Pin refers to one of the tiny leads or "legs" on the chip. Next we use int myVariable = digitalRead(pin). int myVariable creates a new integer variable called myVariable. This new variable is set to 1 or 0 depending on the state of the pin. The ESP32 supply voltage is 3.3V.
We can test out this aspect of microcontrollers very easily. All we need to do is connect an unused pin to either ground or Vss and use the determination to switch something else visually.
In the circuit above the red wire connects to the red connection point rail making this the Vss or 3.3V rail. Wherever we need 3.3V in our future circuits we will connect to this rail. The green wire connects to the blue rail marked negative (-) making this the ground rail. Wherever we need ground or 0V in our future circuits we will connect to this rail.
/*
digitalReadCtrlsLED
M Druiven, Jan.28 2021
*/
int wire = 15; //connect a jumper wire to pin 15
int LED = 2;
void setup() {
pinMode(wire, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int state = digitalRead(wire);
/* state is 1 if the wire is connected to Vss
* state is 0 if wire is connected to ground*/
if (state == 1){
digitalWrite(LED, LOW);
// If the wire is connected to Vss turn the LED OFF
}
else {
digitalWrite(LED, HIGH);
// Otherwise leave the LED ON
}
delay(100); // delay in between reads for stability
}
SAFETY GLASSES ON!
Plug in the USB cable.
When we run this program we should see that the built in LED is ON when the wire (blue) is connected to ground (blue rail). When the blue wire is connected to Vss (red rail) the LED should be OFF.
The program uses an IF structure. Look at it carefully because it can be very useful in the future. It works like this:
if(test) {
do something
}
When the "test" condition is TRUE the program will execute the "do something" lines of code. We always use the "else" command to execute the "default" code.
else {
default code
}
This is not very exciting until we start to add more tests:
if (test) {
do something
}
else if (some other test) {
do something else
}
else {
default code
}
Using else if we can test for multiple conditions and do something different each time.
..... for example, in a smart house
if (smoke and no one is cooking) {
sound alarm
}
else if (smoke and someone is cooking){
open a window
}
else {
blink LED once per minute
}
Unplug the USB cable.
Pushbuttons usually have only 2 connections that are "shorted" when the button is depressed. We can't connect to Vss or ground and we do not want to leave an input connected to nothing. If you digitalRead a pin that is not connected to anything it might be HIGH or LOW - you never know. To overcome this problem we can use a "pull up" or "pull down" resistor.
In this example GPIO32 is normally LOW. It is held low or "pulled down" by the 1K resistor. When the pushbutton is depressed GPIO32 is connected directly to 3.3V, it becomes HIGH. This is good for experimenting because a button push corresponds to a HIGH or 1.
if(button==HIGH) {
do something
}
In this circuit the input GPIO32 is held HIGH via the "pull up" resistor. A button push sends GPIO LOW by connecting directly to ground. This turns out to be more reliable and easier on the microprocessor for industrial and commercial applications. The coding is a little less obvious.
if (button == LOW) {
do something
}
Instead of looking for a 1 or HIGH to do something we wait for a LOW or 0.