In this lesson, you’ll learn how to read digital inputs with the Arduino and Python. For more information on creating a pull down circuit, check out Pull Up and Pull Down Resistors.
Reading Digital Inputs
And the value of
1, which is the high level, is 5V. So, how are you going to use that here with the Arduino? In this example, you’re going to use a push button and the button should send 5V to the board when it’s pressed and send 0V when it’s not being pressed—when it’s released. The LED is going to be connected on digital pin 13, just like you had it before, so none of that’s going to change.
00:41 But you’re going to use a separate circuit going through digital pin 10, which is going to be used as a digital input. In this circuit, you’re going to use the other resistor, the 10 KOhm resistor, and it’s going to act as a pull down in this circuit.
00:55 What a pull down will do is it’ll ensure the digital input gets 0V when the button is released. Let me show you what it looks like. Here’s the existing circuit that you had. Again, coming out of pin 13 as an output, going through the resistor, and connected to the external LED, and then coming back in ground (GND).
01:18 So the LED circuit remains the same. But up top, you can see coming out from the POWER section of the board, you’re taking 5V out, running into the push button, this will be disconnected until the button is pressed.
02:07 And here’s what’s going to be added to it. Again, remember, this is what was existing. Here’s what will be added using the breadboard. Again, coming out of 5, you might remember this red line is where typically positive connections are connected all the way across.
02:23 So you’re going to come out of this 5V power into the positive—I guess you could call it a bus. And then that’s going to run across into the switch. The switch actually will connect across these two rows, but it also connects across these two. So when this is pressed, it will connect across here.
02:45 So this jumper is just simply sending the signal across from, I guess, C6 over here to C11. Then that would go into input 10, digital input 10. This resistor is connected off of that so that again, when this is released and there’s no signal coming in, the 5V coming in, that would then connect over here and then back to ground.
03:25 this small jumper to get into the anode of the LED, cathode coming over to ground, and then ground connecting back in. What’s new? Up here coming out of 5V from the POWER section, going into the positive bus, then jumping into H4, which goes into F4 for the switch, which sends it across to, I guess this would be E6, then D6 across. Then coming out of A11, going right into digital input 10.
And then, line 7 actually starts the
Iterator working, which keeps a loop running in parallel, along with your main code. That loop executes
board.iterate() and updates the input values obtained from the
This is necessary since the default configuration is used to using digital pins as outputs, so you’re changing its role. Here’s where you create another
while loop, so
while True: the switch,
sw, is going to be equal to
board.digital, and you’re going to not write to it, but read from it.
If switch is on, basically—that’ll be
if sw is True: then you’re going to give the digital output 13—which is the LED again, just like before—you’re going to write to it with the value of
1, which again sends out 5V. So that’s the time when it will turn on.
And then otherwise
board.digital, you’re going to write
0 to it. Now, this last line waits
0.1 seconds. So you’re going to say
time.sleep() to basically give it a moment of pause in between looking at the switch and its status. It isn’t strictly necessary, but it’s a nice trick to avoid overloading your CPU, which can reach 100% load if there isn’t any wait state.
Here you’re creating an
Iterator to look at the
board and starting it. So that kind of initializes looking at the board. Then you’re flipping the
.digital to be an input. And here, it’s reading that input—reading, reading, reading, reading—looking for it to be
True, to turn 13 on, or otherwise it will be off.
Great! Go ahead and save. If you haven’t plugged in your board, go ahead and plug it in. It should still have the Firmata sketch loaded onto the board and be ready to receive commands. So go ahead and
07:32 You should see pyFirmata getting set up and establishing some basic communication between the two. And once it’s only the ON LED, you can try out the switch. As you hold the switch down, lighting up the LED, you can see it transmitting and receiving that.
pyFirmata offers a more compact syntax for using inputs and outputs. You set up the
board the same way you have and you call a method
.get_pin(), which accepts a couple parameters as a text string.
The first part of the text string will be the type of pin, where you would put an
a for analog or
d for digital. Then, the pin’s number. Again, in the digital case, this will go from
13 and the analog ones go from
09:20 It Returns an activated pin given by the pin definition. You say what pin do you want—you could say analog or digital, we’ll talk about this later—then the number, and then you can say input or output. So it looks like this.
10:42 and it’s behaving in the same way. Great! In the next lesson, you’re going to start using that potentiometer and start reading in analog inputs, but also using what could be considered analog outputs.
Become a Member to join the conversation.