© Jonathan Bartlett 2020
J. BartlettElectronics for Beginnershttps://doi.org/10.1007/978-1-4842-5979-5_15

15. Analog Input and Output on an Arduino

Jonathan Bartlett1 
(1)
Tulsa, OK, USA
 

In Chapter 14, we learned how to do basic digital input and output with an Arduino using its I/O pins. In this chapter, we will cover how to do analog input and output as well.

15.1 Reading Analog Inputs

So far we have been focusing on digital input and output—HIGH and LOW states. The Arduino Uno also supports some amount of analog input (through its analog pins) and a sort of “faked” analog output (through its pulse-width modulation (PWM) pins, which will be covered in the next section).

On the Arduino Uno, the analog input pins are grouped together in a section labeled “Analog In.” These pins are voltage sensors similar to the digital I/O pins, but they can detect a range of values between 0 V and 5 V (you should never exceed 5 V on any Arduino pin). The analogRead() function is similar to the digitalRead() function in that it takes a pin number and returns an output value. The difference is that the pin number given to analogRead() corresponds to an analog input pin number, not a digital pin number, and the output, rather than being LOW or HIGH, is a number from 0 to 1023 (10 bits of resolution). 0 V will give you a 0, and 5 V will give you 1023. In-between values will give you an in-between result.

Because of this, we can rework the darkness sensor we developed in Chapter 11 to use the Arduino. Since the photoresistor is just a resistor, we still need to make it part of a voltage divider to convert the resistance value to a voltage. However, we no longer need the voltage comparator to get it to work—we can just directly connect it to an analog port on the Arduino!

Figure 15-1 shows the darkness sensor rebuilt for the Arduino. Notice that we have a lot fewer custom components, because the control has been moved from hardware to the Arduino’s software. We don’t need a reference voltage, and we don’t need a voltage comparator. We just have one voltage divider to convert the photoresistor’s resistance to a voltage (with a wire going to the Arduino’s Analog Input Pin 1) and an LED with a current-limiting resistor for the output (fed from Digital Pin 2). Everything else comes from software.
../images/488495_1_En_15_Chapter/488495_1_En_15_Fig1_HTML.jpg
Figure 15-1

The Darkness Sensor Rebuilt for Arduino

Figure 15-2 shows the code that will run the sensor. Note that in the loop() function , we are now using analogRead() rather than digitalRead(). Now, instead of it returning HIGH or LOW, it is returning a number. We can then compare that number to a baseline number to tell us whether we should turn the LED on or off.
../images/488495_1_En_15_Chapter/488495_1_En_15_Fig2_HTML.png
Figure 15-2

Code for the Arduino Darkness Sensor

Now, you may wonder where I got the value to compare against (i.e., 450). What I did was to test the sensor in a variety of conditions and see which value turned off the light when I wanted it to!

However, you may be wondering what exactly the values are that it is reading. Thankfully, the Arduino environment allows a way for us to get feedback from the device while it is running, if it is connected to the computer. To do this, we use what is known as the serial interface to the Arduino. This interface communicates over USB so that we can let our computer know how things are going in the program.

To use the USB serial interface, in your setup function, you add the following line:
Serial.begin(9600);

This tells the chip to initialize its serial interface at 9600 baud (baud is an old term meaning “bits per second”), which allows us to talk back to the computer. However, it is important to note that if you use the Serial functions, you should not have anything connected to Digital Pin 0 or Digital Pin 1 of the Arduino.

Now, in your program, you can do Serial.println() to output any value you want. We will do
Serial.println(analogRead(1));
to let us know what the current value of the analog input is reading at. The new program, with the added feedback, is shown in Figure 15-3. After uploading this code to the Arduino, to see your output, click the magnifying glass on the top right of the screen. You can also go to the “Tools” menu and select “Serial Monitor.” Either way gets you to the same screen. When the code is running, it should be spewing out pages and pages of numbers. Each of these numbers is the current value of analogRead() when it is encountered in the code, which happens hundreds or thousands of times each second (it is slowed down a bit by the USB communication, and you could slow it down even further by adding delay commands at the end of the loop function). This outputting of data in order to better see what is happening within your program is known as debugging and is useful for tracking down problems within your code.
../images/488495_1_En_15_Chapter/488495_1_En_15_Fig3_HTML.png
Figure 15-3

Darkness Sensor with Serial Feedback

15.2 Analog Output with PWM

So we have discussed analog input, but what about analog output? Truthfully, the Arduino does not support analog output as such. However, analog output is faked on an Arduino using a technique known as pulse-width modulation , abbreviated as PWM . The Arduino only outputs 5 V on its output pins. But let’s say we wanted to fake a 2.5 V signal. What might we do? Well, if we turned the pin on and off rapidly so that it was only on for half the time, that would give us about the same amount of electrical power as a continuous output of 2.5 V. That’s what PWM does—it fakes lower voltages by just flipping the power to the pin on and off very rapidly so that it “looks” like a lower voltage. In Chapter 22, we will see how to actually convert this to a “true” voltage.

Arduino programs use the function analogWrite() to use a pin for PWM. This function is a little confusingly named because (a) it uses digital pins, not analog pins, and (b) the value is between 0 and 255, not 0 and 1023 like analogRead(). Other than that, it basically does what you might expect. analogWrite(3, 0); will turn off Digital Pin 3, analogWrite(3, 255); will turn it all the way on, analogWrite(3, 127); will flick it on and off pretty evenly, and analogWrite(3, 25); will keep Digital Pin 3 on only a short time relative to how long the pin stays off.

To get a flavor for PWM, we will do a very simple PWM project—a dimmed LED. Figure 15-4 shows what the connection will look like—just an LED with a current-limiting resistor attached to Digital Pin 3 (which is marked with a ˜ to indicate that it is capable of PWM). Figure 15-5 shows the code to dim the output.
../images/488495_1_En_15_Chapter/488495_1_En_15_Fig4_HTML.jpg
Figure 15-4

A Simple Analog Dimmer

../images/488495_1_En_15_Chapter/488495_1_En_15_Fig5_HTML.png
Figure 15-5

Code for the Analog Dimmer

This code is a little more complicated. It’s alright if you don’t understand it fully, as this isn’t a programming book. But I think if you think about it long enough, you can get it.

In short, it creates a variable , which is a named temporary storage location for a value (we are calling it i for a short name). It is declared an int, which means it will hold an integer, and we set it with a starting value of zero.

The while command means that this will repeat (or loop), executing everything within the block of code between { and } over and over again, as long as i is less than 255. Within this block, we write the value of i to pin 3 using analogWrite(). Then, we delay for 10 milliseconds to make sure the change is visible (you can adjust this value to the speed you want the brightness to change at). Then, we increase i by one to go to the next value.

The next while loop does the same thing but goes the other way. It starts at 255 and progresses down to 0. Then, when it is all the way to zero, the loop() function completes. Then, the Arduino runs the loop function over again.

Even though the Arduino is outputting “analog” by switching the pin on and off at different rates, it looks like the LED is dimming on and off. It is flickering so fast that we merely perceive it as a lower-energy light than as a pulsing light. In fact, when it gets to about 180 (about 70% on and 30% off), there is not a lot of difference between that and full brightness.

15.3 Review

In this chapter, we learned the following:
  1. 1.

    The Arduino can read voltage values between 0 V and 5 V through the analog input pins, using the analogRead() function .

     
  2. 2.

    The values given from the analog input pins will be integers between 0 and 1023, where 0 represents 0 V, 1023 represents 5 V, and the numbers in between represent in-between voltages.

     
  3. 3.

    When using resistive sensors, you still need to incorporate the resistive sensors into a voltage divider circuit in order to convert the varying resistance into a varying voltage.

     
  4. 4.

    We can send debugging information back to the Arduino IDE with the USB serial interface, using the Serial object and its related commands.

     
  5. 5.

    Analog output on Arduino is done with the Arduino digital pins (not the analog pins), but only on the ones labeled with a tilde (˜), using the analogWrite() function .

     
  6. 6.

    Analog output on Arduino is done using pulse-width modulation (PWM), which switches the output pin on and off very rapidly to simulate a partial voltage.

     
  7. 7.

    The analog level of the analog output on the Arduino is specified by a number between 0 and 255, where 0 means no voltage, 255 means to keep the pin at 5 V the whole time, and the numbers in between cause the pin to switch back and forth rapidly to emulate a voltage between the two values.

     
  8. 8.

    In computer programming, a variable is a temporary storage location for a value.

     
  9. 9.

    In computer programming, a while loop repeats the inside of the while loop over and over again until its condition is no longer valid.

     

15.4 Apply What You Have Learned

  1. 1.

    What do you have to do to a resistive sensor in order to read the value of the sensor on the analog input?

     
  2. 2.

    In the code that dimmed the lights, how would we slow down the dimming process?

     
  3. 3.

    Modify the darkness sensor circuit and code so that it will output a dimmed (analog) value to the LED.

     
  4. 4.

    Further modify the darkness sensor circuit so that, at different darkness levels, the LED has different levels of brightness.

     
  5. 5.

    Think of your own modifications to the circuits in this chapter. Come up with a new way of putting together the pieces to which you have learned to create some amount of modified functioning. Implement this new design.

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.91.0.68