Analog Inputs and Outputs
In this lesson, you’ll learn about using analog inputs and outputs with the Arduino and Python.
For more information about the analog circuits used in this lesson, check out the following resources:
00:00 In this lesson, you’re going to work with analog inputs and how to simulate using analog outputs. Analog inputs are used to read a range of values. For the Arduino, the analog inputs read a range from 0V up to 5V.
00:17 To read those voltages, the Arduino uses an ADC, an analog-to-digital converter, which converts input voltage to a digital number with a fixed number of bits.
00:29 Those number of bits will determine the resolution, or quality, of the conversion. The board that I’m using is the Arduino Uno, and it has a 10-bit ADC, so it can determine 1024 different voltage levels, or 2 to the 10th power. To create those voltage differences, you’re going to use a potentiometer, which is a variable resistor.
00:51 The one you’re going to use in this example is a 10 KOhm potentiometer. The potentiometer has three terminals. Two of the terminals are connected to both ends of a resistive element, and then the third terminal—which is in the middle—connects to a sliding contact that’s called a wiper.
01:08 The potentiometer can be used to set the voltage applied to an analog input. In the code script you’re going to write for your Arduino, you’re going to use the potentiometer and the conversion of that voltage to control the frequency of the blinking LED—basically, how fast or slow it will blink. For connections, if you were to remove the connections you added for the switch and just strip it back down to where you had the LED only, to that you’re going to add the potentiometer. For those three terminals on the potentiometer, you’re going to go across the resistive element with 5V coming in one side, and then to the other side, connecting to ground (GND). The wiper—the middle pin, if you will—is going to be connected into analog input 0.
01:55 So as you turn the wiper back and forth, the resistance is going to change the voltage coming in. Okay. Again, that would be if you were soldering it up—what’s it look like on the breadboard?
02:07 Here’s the circuit for the LED. To add the potentiometer, it would look something like this. Again, coming out 5V, that 5V connection is coming into the positive bus.
02:19 That signal’s going into this first terminal, then coming out of the last terminal is going into ground (GND), which this is still connected here, on ground.
02:30 And then the middle terminal, the wiper—or I guess sometimes it’s called the cursor—is actually what’s going back into analog 0. Let me show you how it turned out on my board. Inserted into column A of the breadboard, you can see the potentiometer’s terminals in A4, the middle terminal on A6, and the last terminal on A8.
03:02 The LED connections have remained the same, so I’m not going to go too far into them. Let me straighten out this LED so it looks right. So now, on the other side of the board, from the POWER section, you’re coming out 5V and going into that positive bus.
03:17 And then that positive connection with this brown jumper here is going into row 4 from E4, which is connected to the first terminal, A4. The middle terminal is coming out of row 6, A, from D6, all the way back over here to analog input A0.
03:35 And on the other side of the resistor element for the potentiometer you’re coming out on row 8 from A8 to E8 into the ground bus. Your next step is to create a new script that’s going to look at that analog input and read the numbers from it.
03:51
Initially, you’d just put them on the screen. I’m going to go ahead and create a new file. It’s going to be called analog_in.py
. It’s going to use a lot of the same stuff from switch.py
, so I’ll just copy it over
04:10
and save. Okay. So again, still using pyfirmata
, still using time
, you still have your board
set up here. I’m just going to reduce this a little bit so these are tighter together.
04:24
You’re going to have that Iterator
, still looking at the board
and starting the Iterator
. Okay. Let’s just go ahead and erase the rest here.
04:33
Line 8. You’re going to create an analog_input
.
04:39
So from board
, you’re going to get a pin and as you might have recognized, you’re using analog input 0, so 'a:0:'
as an input ('i'
).
04:54
Here’s your while
loop. while True:
analog_value
, and it’s going to be equal to the analog_input
and you’re going to get a read from it. It’s a little different here. Again, instead of just 0s and 1s, let’s print that analog_value
out. Again, you’ll do a simple sleep()
. Do it for that 10th of a second again. Great.
05:15
Go ahead and save and let’s try it out. So here, python
05:28 Maybe make this terminal window a little larger. Okay, as I turn the dial here—1,
05:38
dialing down to 0. Yeah, this looks like it’s working great. Cool. So you can see all those values as a float coming in from 0.0
05:53
to 1.0
. Great! Go ahead and interrupt that, Control + C. So now to control that LED, go ahead and add it here. I’m going to go ahead and change the size of the terminal here.
06:06
So, along with analog_input
, you’re going to have an led
. And you remember this, right? Get the pin. It’s a digital pin, 13, and it’s acting as an output—an 'o'
. So here, now I’ve got to modify this a little bit, adding an if analog_value is not None:
06:28
as long as there is a value there, you’re going to create a delay
and that’s going to be equal to analog_value + 0.01
, so at least there’s some time there.
06:38
And then you’re going to .write()
to the led
with a 1
, turning it on. And then you’re going to do a sleep()
here—time.sleep()
—and that’s going to be the delay
amount.
06:49
Then to have it blink off, .write()
with a value of 0
, and then the sleep()
will be the delay
amount again.
07:03
you’ll have it do the time.sleep()
here. So again, you’re reading the analog_input
, it’s saving it into analog_value
.
07:09
As long as analog_value
has some kind of value in it, it’s not None
—which should typically be True
, right?—you’re going to add analog_value
just to have some kind of value here so it’s not just 0 and constantly flashing.
07:22
And then you’ll turn on the LED, wait the delay
amount, turn off the LED, and then again, just loop around based upon that. Looks good! I’m going to save.
07:35 Go ahead and run your script. On your Arduino board, you should see some LED flashing, showing pyFirmata talking to the Firmata on the board. Then the script should start running. In this case, it is really high, so it’s a much slower rate.
07:52 And as I turn the potentiometer, you can see the rate increasing to the point where it’s flashing extremely fast. And then again, adjusting to make it much slower again.
08:05 And notice that as the potentiometer’s turned, it’s transmitting those changes, and then also receiving the changes back from the script. Great! Looks like it’s working good.
08:21 So, I mentioned that you were also going to play around with analog outputs, but can the Arduino provide an analog output? Well, it doesn’t have a real analog output, but there’s something that can simulate that. An analog output would be where the voltage could be set to any value within that certain range.
08:39 The way the Arduino can sort of accomplish something like that is using what’s known as a Pulse Width Modulation output, or PWM output. PWM is a modulation technique, and it’s where a digital output is used to generate a signal of variable power.
08:58 It uses a digital signal of a constant frequency, so this frequency is going to stay the same, but the duty cycle is changed according to what you want the desired power to be. The duty cycle represents the fraction of the period of time which a signal is set to high.
09:15 This can accomplish a simulation of an analog output. You can find links below this lesson to learn more about duty cycle and PWM. You’re going to use yours to control the brightness of the LED, just like, how much voltage would go to it. In this case, it’s going to be controlled by that duty cycle. On the Arduino board, you’ll see that some of the digital connections have a symbol next to them.
09:43
It looks like a tilde (~
), a little squiggle. That symbol is indicating those that are actually the PWM outputs: 3, 5, 6, 9, 10, and 11. Here’s the circuit you had set up just a moment ago in the previous example.
09:58 What you’re going to do is change pin 13 on the digital output, and you’re going to move it over to pin 11, which is one of the PWMs, and you can see that here. So again, on the breadboard example, connect it to here, and you’re going to move that over here to that PWM output number 11.
10:19 I’ll do the same thing here. Move it from pin 13 over to pin 11 and press it in.
10:30 Looks good. Now the big change you need to make is in your code.
10:35
Okay. So you’ve got this program called analog_in.py
. I’m going to have you make a new script.
10:43
It’s going to be called pwm_out.py
. It’s going to use a lot of analog_in
, so go ahead and copy that over. That way we don’t have to repeat too much code here. Save, okay.
10:57
Still importing pyfirmata
, still importing time
, still setting up your board
, still setting up the Iterator
—that all looks good.
11:04
Still need that analog_input
. Again, that’s the potentiometer control. That looks good. But this time the led
is going to be what you’ve changed here to pin 11.
11:17
And instead of an output, it’s going to be not an input or an output, but a PWM—a 'p'
. Okay, great. You’re still getting that analog_value
from your analog_input
reading, and again, you still want to check to make sure that it’s not None
, and you’re going to .write()
to the led
whatever that analog_value
is. And again, within that while
loop, still want to have that sleep()
so it doesn’t overwhelm the CPU. It’s actually a little smaller. Looks good!
11:45 Let’s go ahead and run it.
11:50 Okay. You can see pyFirmata sending the data over. And now as you adjust the potentiometer, the brightness of the LED should increase and decrease.
12:03 You can see all those values being transmitted and received,
12:09 simulating an analog output. Looks great!
12:14 In the next lesson, you’re going to reuse the circuit you created for reading digital inputs to trigger notifications.
Become a Member to join the conversation.