Arduino analog input as output

Tutorial 20: Arduino Analog Input / Output

Arduino Analog Input / Output

I often wonder who would win if Frankenstein’s Monster and Dracula got into a fight. My personal bet is on the monster – but I would be concerned with Dracula doing some funky vampire mind control trick.

Now this lesson doesn’t use mind control, but it does use Arduino analog input to control the output of a digital pin. Controlling a digital pin with an analog input can be extremely helpful because many sensors report analog data.

This lesson explores how to use analog input to adjust the intensity of an LED.

You Will Need

  1. LED (1)
  2. 220-Ohm Resistor (1)
  3. Potentiometer (1) any resistance
  4. Jumper Wires (3)
  5. Alligator Clip (1)
  6. Juice mix (12 ounces)

Step-by-Step Instructions

  1. Place the potentiometer into the breadboard pictured in the circuit diagram.
  2. Run a jumper wire from the 5-volt pin of the Arduino to either one of the outside pins of the potentiometer.
  3. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer.
  4. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer.
  5. Place one end of the 220-ohm resistor into pin 9.
    Place the short leg of the LED into the ground pin next to pin 13.
  6. Connect the long leg of the LED to the other end of the resistor with the alligator clip.
  7. Plug the Arduino into your computer.
  8. Open the Arduino IDE.
  9. Open the sketch for this section.
  10. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished.
  11. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished.
  12. Open the serial monitor window – sensor input and output data should be streaming along.
  13. As you adjust the knob of the potentiometer the brightness of the LED should vary.

The Arduino Code

Discuss the Sketch

This sketch begins by declaring and initializing two variables, one for the potentiometer input pin and one for the LED output pin.

The variables are qualified as constants which keeps them safe from being changed anywhere else in the program:

The next variables created are integers used for storing input and output data:

The setup() of this function is simple, we initiate serial communications with the Serial.begin() function:

Now we proceed to the last block of code, the loop().

The loop() first reads the value at analog pin A0 (where the potentiometer is attached) and assigns this value to a variable named sensorValue:

Recall that analogRead() returns a value between 0 and 1023. In this example, we use the input value we just recorded as the output value for the analogWrite() function. But wait a minute! The analogWrite() function will only accept values in the range of 0 through 255…the value we record with analogRead() can be much larger.

One solution to this problem is to rescale the input values to within the range of the analogWrite() function. The map() function does this easily:

Recall that map() takes 5 arguments:

In this case, we use the map() function to adjust the input value to correspond with the range of analogWrite(). Now that we have a value that will work with analogWrite(), let’s go ahead and get started with the output:

As you may recall, analogWrite() takes two arguments 1) a pin number 2) the value to write. This function utilizes pulse width modulation, allowing you to adjust the power output of the PWM enabled pin (in this case pin 9 where we have the LED attached).

Recapping what we have done:

  1. Read the input from analog pin A0
  2. Adjusted the value to a condensed range using the map() function
  3. Passed the value to analogWrite() which in turn adjusts the brightness of the LED

Pretty simple – highly useful.

But what is a program without some type of data output? We get our fix by writing the input and output values to the serial port.

The \t in the third statement will insert a into the string. Notice how the last Serial.println() function is different – it has “println” instead of just “print”. The println() function prints the outputValue variable and then starts a new line in the serial monitor window – this way all the information lines up nicely. A newline is like pressing the return key in a word editing program – it moves to the next line.

The last line of the sketch is the delay() function, which allows the analog-to-digital converter to stabilize before we feed it another input at the top of the loop().

Let’s review what this loop() does:

  1. Reads an input value at analog pin A0
  2. Assigns the value to a variable
  3. Changes the input value to match the scale of the output’s allowable range
  4. Assigns the newly mapped value to a variable
  5. Feeds the variable to analogWrite() to adjust the brightness of an LED
  6. Delays the program for 2 milliseconds to ensure the next reading is stable

Источник

Analog Input

In this example we use a variable resistor (a potentiometer or a photoresistor), we read its value using one analog input of an Arduino board and we change the blink rate of the built-in LED accordingly. The resistor’s analog value is read as a voltage because this is how the analog inputs work.

Hardware Required

Potentiometer or

10K ohm photoresistor and 10K ohm resistor

built-in LED on pin 13 or

220 ohm resistor and red LED

Circuit

With a potentiometer

With a photoresistor

Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 0 to the middle pin of the potentiometer.

For this example, it is possible to use the board’s built in LED attached to pin 13. To use an additional LED, attach its longer leg (the positive leg, or anode), to digital pin 13 in series with the 220 ohm resistor, and it’s shorter leg (the negative leg, or cathode) to the ground (GND) pin next to pin 13.

The circuit based on a photoresistor uses a resistor divider to allow the high impedence Analog input to measure the voltage. These inputs do not draw almost any current, therefore by Ohm’s law the voltage measured on the other end of a resistor connected to 5V is always 5V, regardless the resistor’s value. To get a voltage proportional to the photoresistor value, a resistor divider is necessary. This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors. The voltage measured (Vout) follows this formula:

Vout=Vin*(R2/(R1+R2))

where Vin is 5V, R2 is 10k ohm and R1 is the photoresistor value that ranges from 1M ohm in darkness to 10k ohm in daylight (10 lumen) and less than 1k ohm in bright light or sunlight (>100 lumen).

Schematic

At the beginning of this sketch, the variable sensorPin is set to to analog pin 0, where your potentiometer is attached, and ledPin is set to digital pin 13. You’ll also create another variable, sensorValue to store the values read from your sensor.

The analogRead() command converts the input voltage range, 0 to 5 volts, to a digital value between 0 and 1023. This is done by a circuit inside the microcontroller called an analog-to-digital converter or ADC.

By turning the shaft of the potentiometer, you change the amount of resistance on either side of the center pin (or wiper) of the potentiometer. This changes the relative resistances between the center pin and the two outside pins, giving you a different voltage at the analog input. When the shaft is turned all the way in one direction, there is no resistance between the center pin and the pin connected to ground. The voltage at the center pin then is 0 volts, and analogRead() returns 0. When the shaft is turned all the way in the other direction, there is no resistance between the center pin and the pin connected to +5 volts. The voltage at the center pin then is 5 volts, and analogRead() returns 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

That value, stored in sensorValue , is used to set a delay() for your blink cycle. The higher the value, the longer the cycle, the smaller the value, the shorter the cycle. The value is read at the beginning of the cycle, therefore the on/off time is always equal.

See Also:

AnalogInOutSerial — Read an analog input pin, map the result, and then use that data to dim or brighten an LED.

AnalogWriteMega — Fade 12 LEDs on and off, one by one, using an Arduino Mega board.

Calibration — Define a maximum and minimum for expected analog sensor values.

Fading — Use an analog output (PWM pin) to fade an LED.

Smoothing — Smooth multiple readings of an analog input.

Источник

Analog Input Pins

LAST REVISION: 10/05/2022, 01:00 PM

A description of the analog input pins on an Arduino chip (ATmega8, ATmega168, ATmega328P, or ATmega1280).

A/D converter

The ATmega controllers used for the Arduino contain an onboard 6 channel (8 channels on the Mini and Nano, 16 on the Mega) analog-to-digital (A/D) converter. The converter has 10 bit resolution, returning integers from 0 to 1023. While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 — 13).

Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.

Pin mapping

The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:

Pull-up resistors

The analog pins also have pull-up resistors, which work identically to pull-up resistors on the digital pins. They are enabled by issuing a command such as

Be aware however that turning on a pull-up will affect the values reported by analogRead().

Details and Caveats

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pull-up resistor will be set, when switched back to an input.

The ATmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

Источник

Analog In, Out Serial

This example shows you how to read an analog input pin, map the result to a range from 0 to 255, use that result to set the pulse width modulation (PWM) of an output pin to dim or brighten an LED and print the values on the serial monitor of the Arduino Software (IDE).

Hardware Required

220 ohm resistor

Circuit

Connect one pin from your pot to 5V, the center pin to analog pin 0 and the remaining pin to ground. Next, connect a 220 ohm current limiting resistor to digital pin 9, with an LED in series. The long, positive leg (the anode) of the LED should be connected to the output from the resistor, with the shorter, negative leg (the cathode) connected to ground.

Schematic

In the sketch below, after declaring two pin assignments (analog 0 for our potentiometer and digital 9 for your LED) and two variables, sensorValue and outputValue , the only things that you do in the setup() function is to begin serial communication.

Next, in the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

See Also:

AnalogInput — Use a potentiometer to control the blinking of an LED.

AnalogWriteMega — Fade 12 LEDs on and off, one by one, using an Arduino Mega board.

Calibration — Define a maximum and minimum for expected analog sensor values.

Fading — Use an analog output (PWM pin) to fade an LED.

Smoothing — Smooth multiple readings of an analog input.

Источник

Analog Input Pins

A description of the analog input pins on an Arduino chip (ATmega8, ATmega168, ATmega328P, or ATmega1280).

A/D converter

The ATmega controllers used for the Arduino contain an onboard 6 channel (8 channels on the Mini and Nano, 16 on the Mega) analog-to-digital (A/D) converter. The converter has 10 bit resolution, returning integers from 0 to 1023. While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 — 13).

Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.

Pin mapping

The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:

Pull-up resistors

The analog pins also have pull-up resistors, which work identically to pull-up resistors on the digital pins. They are enabled by issuing a command such as

Be aware however that turning on a pull-up will affect the values reported by analogRead().

Details and Caveats

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pull-up resistor will be set, when switched back to an input.

The ATmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

Источник

Adblock
detector