Reading Digital Inputs
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.
00:00
In this lesson, you’ll take the next steps and start reading digital inputs. Digital inputs can have only two possible values. The value of 0
, considered the low level, has a voltage of 0V.
00:14
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.
01:35 When pressed, that signal will go into input 10. But when released, this resistor will make sure that—again, this is connected back to ground (GND)—that 10 goes back to a value of 0V.
01:48 So again, when pressed, it’ll get 5V in this way, but when released, it’ll make sure with this resistor that it’s back to 0V. So again, you could wire this up using solder, if you wanted to, or
02:02 here’s your old circuit using the breadboard.
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:14 Let me show what it looks like in how I’ve set it up on my board. It’s pretty close. I haven’t changed anything about the LED setup. So again, coming out of 13, going into the resistor,
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.
03:58 But also there’s the resistor that then is sending it back to ground, and the ground bus is connected still through the connection there. Okay, time to look at the code.
04:10
Go ahead and close the blink
script that you wrote earlier and create a new one and call it switch.py
.
04:23
It’s going to start out pretty similarly. Start by importing pyfirmata
and time
. And again, you need to create the same board
04:36 with the same connection.
04:42
This time you’re going to change something a little bit. You’re going to create an iterator. It’s going to be called it
. And from pyfirmata.util
, you’re going to use Iterator()
from the board
,
04:56
and then you’re going to use the method .start()
. Let me go over this again. On line 6, you’re assigning an Iterator
that’s going to be used to read the status of all the inputs of the circuit.
05:07
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 board
.
05:21
And this time you’re going to use board.digital[10]
—again, input 10—and look at its .mode
as an input. So again, line 9 is going to set pin 10 as a digital input with pyfirmata.INPUT
.
05:37
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[10]
, and you’re going to not write to it, but read from it.
05:56
If switch is on, basically—that’ll be True
right? 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.
06:14
And then otherwise board.digital[13]
, 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.
06:40
This is just going to give a little bit of pause there. So again, importing pyfirmata
, importing time
, creating your board
with the same port.
06:48
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[10]
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.
07:12
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 python
run switch.py
.
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.
07:53 Great! Awesome! And again, it’ll keep running until you tell it to quit, which you can break out of it with Control + C. Awesome.
08:03
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.
08:16
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 0
to 13
and the analog ones go from 0
to 5
.
08:30
Then the mode of the pin—is it acting as an input, or is it acting as an output? So, i
for input and o
for output. So put that all together and it would look something like this.
08:40
You’re naming an object called digital_input
, and it’s based upon board.get_pin()
digital pin 10
, acting as an input. Again, notice the colons (:
) in between creating this string.
08:55 You’ll get to practice this a few times here. Let’s change your code out to use this more compact syntax, which you’ll use for the rest of this course.
09:05 So you’re going to change here at line 9,
09:12
creating what’s called digital_input
. From board
you’re going to .get_pin()
and here’s where that new syntax comes in.
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.
09:33
We want it to be a digital pin, it’s number 10
, and then it’s going to act as an input.
09:39
The led
, we’ll define here using .get_pin()
also—it is also a digital pin. It’s number 13
, but it’s acting as an output.
09:48
So then here in your while True:
the switch is digital_input
and you’re reading it, and here, change this to just be led
, and then you’re writing it. And the same here, this will be led
also.
10:03
So a couple of changes. Again, defining your digital_input
here using this new syntax board.get_pin()
, and again, defining your led
here in kind of the same way.
10:14
The switch is read. Again, if switch is True
, light up the led
or make sure it’s off. Great! Go ahead and save that, and you can run it again just to test it.
10:34 Here it is receiving the Firmata instructions. And test out the switch,
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.
dariovignali on May 19, 2024
I seem to have problems usign Python3 together with pyfirmata. I think I need to use pyfirmata2 but the commands are slightly different. Should this course be updated? Or am I doing something wrong? Thanks