Вы находитесь на странице: 1из 10

For those of you that don’t need any hand holding…

Analog I/O: Summary


Arduino IDE Commands:

analogWrite(x,value); // produce PWM signal at pin x with duty cycle value/255 
analogRead(Ax); // read analog voltage at Ax pin 
analogReference(VALUE); // change Vref see link below for description 

Arduino Documentation:

PWM Tutorial: https://www.arduino.cc/en/Tutorial/PWM


Analog Input Pins: https://www.arduino.cc/en/Tutorial/AnalogInputPins
analogWrite: https://www.arduino.cc/en/Reference/AnalogWrite
analogRead: https://www.arduino.cc/en/Reference/AnalogRead
analogReference: https://www.arduino.cc/en/Reference/AnalogReference

Remember: Always set the pin as an input or output before using it.

Digital I/O: Summary


Arduino IDE commands:
 
pinMode(x,INPUT); // set pin x as input 
pinMode(x,INPUT_PULLUP); // set pin x as input with pullup resistor (10k) 
pinMode(x,OUTPUT); // set pin x as output 
digitalWrite(x,HIGH); // write HIGH (5v, source) at pin x 
digitalWrite(x,LOW); // write LOW (0v, sink) at pin x 
digitalRead(x); // read logical voltage at pin x 

Arduino Documentation:

Digital Pins: https://www.arduino.cc/en/Tutorial/DigitalPins


pinMode: https://www.arduino.cc/en/Reference/PinMode
digitalWrite: https://www.arduino.cc/en/Reference/DigitalWrite
digitalRead: https://www.arduino.cc/en/Reference/DigitalRead

Remember: Always set the pin as an input or output before using it.

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 1 of 10

 
Arduino IDE Programming Structure & (very) Basic Notes on C/C++

The basic structure of an Arduino Sketch includes a setup and loop function. These functions are
required in all sketches, and the program will not compile without them. The purpose of these functions
is similar to how we wrote our programs in EE346. The setup sketch is run once after the microcontroller
boots up, and once it is finished, it runs the loop function. The loop function is run over and over
indefinitely until the microcontroller is turned off, reset, dipped in molten lead, waterboarded, set on fire,
etc.

The curly brackets { } are the bounds of any subroutine or block of code that is to be executed. If you
were to place a command outside of the setup brackets, it will not be executed as a part of the setup
function.

Note: Each C/C++ command MUST be terminated with a semicolon ( ; ). If this is missing, the program will
not compile and the error window will give you an error message: 
 

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 2 of 10

 
Some More Notes on C/C++: Variables
This is a great website with documentation, guides, and examples on everything you didn’t know that you
didn’t know about C++: ​ http://www.cplusplus.com/​
. Here is a great forum for all things AVR:
http://www.avrfreaks.net/

Variables
Variables need to be declared using the following syntax:

type identifier; // declare a variable 
type identifier = initialValue; // declare and initialize a variable 
type identifier {initialValue}; // declare and initialize a variable (alternate) 
 
If you are a programming noob, read up on data types here: ​
http://www.cplusplus.com/doc/tutorial/variables/

Type Conversion
Sometimes you may run into trouble where a variable’s type interferes with a value you try to set it to. You should get an
idea of how this works by checking out this link: ​
http://www.cplusplus.com/doc/tutorial/typecasting/
 
Variable Scope
Variable scope refers to the ability to access certain variables in different sections of the program. For example, variables
declared in the setup function are unknown outside of the setup function. Below is an example, but check the link if you
need more information: ​ http://www.cplusplus.com/doc/tutorial/namespaces/

 
Order of Operations
When using arithmetic operations, there is a particular order in which they are executed. When in doubt, use
parentheses. If you have no idea what I’m talking about, Please Excuse My Dear Aunt Sally:
http://www.cplusplus.com/forum/beginner/168336/

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 3 of 10

 
Digital I/O: Pin Modes
All numbered pins on the Arduino board can function as digital I/O pins. This includes pins 0 through 13
as well as the Analog In pins A0 through A5. Digital I/O pins can be used to read or write logical voltage
levels (5v = HIGH, 0v = LOW).

Image Source: ​
https://github.com/Bouni/Arduino-Pinout

In order to use the pins effectively, you must declare them as an input or an output using one of the
following commands:

pinMode(x,INPUT); // x is the pin number 
pinMode(x,INPUT_PULLUP); // pin x is input with 10k ohm pullup resistor 
pinMode(x,OUTPUT); 
 
Where x is the pin number 0-191 . These commands modify the GPIO registers associated with the pin to
set them as an input or output, respectively. Adding a pullup resistor is a good
 
Note: Syntax matters. The​ ​
pinMode​ text should turn orange to indicate that you are using a valid
Arduino-defined function. Similarly,​
 ​
INPUT​ and​
​  ​
OUTPUT​ should turn green/blue because they are
 ​
predefined values in Arduino IDE.

1
When used as digital pins analog ​
 ​ channels A0-A5 are automatically mapped to digital pins 14 to 19​

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 4 of 10

 
Digital I/O: Digital Input & Output
By using the following command on a pin that was configured as an output, you can set a constant voltage
on a digital pin:

digitalWrite(x,HIGH); // set voltage at pin x to HIGH (5v, source) 
digitalWrite(x,LOW); // set voltage at pin x to LOW (0v, sink) 

Note: Arduino’s digital pins can sink/supply about 20mA max current. This is plenty to power an LED with a
series resistor (typically 1 Kohm), but may cause damage if you are trying to power a motor or servo. The
pins on most microcontrollers are designed for sending signals, not supplying power.

After setting the pin as an input, you can use the following function to read the voltage level at the pin.

digitalRead(x); // x is the pin number 
 
This command returns a 1 or 0 depending on the voltage level. That means that if the pin is high,
digitalRead(x) would be the same as writing 1. Mid range voltages are quantized to a high or low value
for input depending on the logic threshold voltage of the board (typically around 3v). That means that
3.7v would register as HIGH and 2.1v would register as LOW.

You can save the result of the command and use it later in the program. The following commands are
valid uses of digitalRead(x);
 
digitalRead(x);  // value is not saved/used, but the command is executed 
a = digitalRead(x);  // result is saved into variable a 
if (digitalRead(x)) {a=b+c;}  // if pin x is high, execute the command a=b+c; 
 
Digital inputs are very useful for conditional statements for controlling the flow of a program. Consider
the following code:

a=digitalRead(5); 
b=digitalRead(6); 
if ((a==1)&&(b==1)) // alternatively, if (digitalRead(5) && digitalRead(6)) 

digitalWrite(7,HIGH); 

The code above is the equivalent of an AND gate with inputs a and b, and output c. If both pins 5 and 6
are high, set pin 7 to output high.

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 5 of 10

 
Digital I/O: Summary
Arduino IDE commands:
 
pinMode(x,INPUT); // set pin x as input 
pinMode(x,INPUT_PULLUP); // set pin x as input with pullup resistor 
pinMode(x,OUTPUT); // set pin x as output 
digitalWrite(x,HIGH); // write HIGH (5v, source) at pin x 
digitalWrite(x,LOW); // write LOW (0v, sink) at pin x 
digitalRead(x); // read logical voltage at pin x 

Arduino Documentation:

Digital Pins: https://www.arduino.cc/en/Tutorial/DigitalPins


pinMode: https://www.arduino.cc/en/Reference/PinMode
digitalWrite: https://www.arduino.cc/en/Reference/DigitalWrite
digitalRead: https://www.arduino.cc/en/Reference/DigitalRead

Remember: Always set the pin as an input or output before using it.

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 6 of 10

 
Analog I/O: Analog Output and PWM

Certain Pins can be used to produce an analog output (digital pins with the ~ sign next to the number, 3, 5,
6, 9, 10, 11 on UNO) in the form of a pulse-width modulated (PWM) signal. Since the microcontroller
operates with digital logic and ones and zeroes, it doesn’t have the ability to easily output truly analog
signals. In order to produce a pseudo-analog value, PWM is used to change the duty cycle (%duty) of a
repeating digital signal.

%duty  =   t ton
period

Arduino uses the following function to create a PWM signal on an analog output pin:

analogWrite(x,value); // write PWM signal based on value 

Where value is an integer between 0 and 255 and the duty cycle is calculated by value/255. See the figure
below for examples.

Note: the pin on which the PWM signal is produced should be set as an output

Image Source: ​
https://www.arduino.cc/en/uploads/Tutorial/pwm.gif

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 7 of 10

 
Analog I/O: Analog Input
Arduino’s analog input pins (pins with an ‘A’ before the number, A0 - A5) can be used to read an analog
voltage. This is done using a 10-bit analog to digital converter (ADC), which converts an analog voltage
into a 10-bit integer (0 - 1023). The number is based on the reference voltage (5.0v by default on UNO),
where if the input voltage is at or above the reference, the value returned is 1023. Alternatively, reading
a grounded or negative voltage will return 0. The formula for analog to digital conversion is as follows:

V
x = round( V in * (2b − 1))
ref

Where x is the returned value, Vref is the reference voltage, and b is the bit-number of the ADC. Example
for 10-bit ADC:
10
round(2.45v
5.0v * (2 − 1))  =  501  

The resolution of an ADC depends on the number of bits it returns as well as the input voltage:

V ref
res = 2b−1

For the Arduino UNO, the resolution is roughly 4.9 mV. Higher resolutions can be achieved by changing
the settings for the reference voltage of the Arduino (if you need to monitor a smaller voltage range more
accurately). Read more on this here: ​ https://www.arduino.cc/en/Reference/AnalogReference​ .

The analog input pins can be used to read an analog voltage using the following command:

analogRead(Ax); // read analog value at Ax pin (ex. A0) 
 
Note: pins used for an analog reading should be set as an input.

This function returns an integer value between 0 and 1023. This is the bread and butter of reading
sensors. Some sensors are useful for getting a yes/no reading (ex. switches), but for many sensors
(temperature, pressure, force) it’s useful to get readings on mid-range voltages. Once you take the analog
reading, it can be scaled and viewed to extract meaningful data. Below is sample code for scaling an
analog reading to a voltage range between 0 and 5.

reading = analogRead(A0); // read analog value at A0 and save to a variable 
voltage = 5.0*reading/1023.0; // convert reading into a true­value voltage 
 
Note: Look at the output of a PWM signal from the analogWrite section. Analog readings happen very
quickly (around 0.1ms). If you were to try to read an analog value from a pin that is producing a PWM
signal, you will most likely get 0 or 1023 because the PWM signal is not truly analog. 
 
 
   

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 8 of 10

 
Analog I/O: Analog Input (cont.)
 
An easy way to practice using the analogRead function is to use a potentiometer. By turning the dial, you
can set an analog voltage at the scrubber pin. This is also a great way easily control a value in your
program to help with tuning a design.

Image Source: ​
http://dm.risd.edu/pbadger/PhysComp/index.php?n=Devices.Potentiometer

Analog I/O: Summary


Arduino IDE Commands:

analogWrite(x,value); // produce PWM signal at pin x with duty cycle value/255 
analogRead(Ax); // read analog voltage at Ax pin 
analogReference(VALUE); // see link below for description 

Arduino Documentation:

PWM Tutorial: https://www.arduino.cc/en/Tutorial/PWM


Analog Input Pins: https://www.arduino.cc/en/Tutorial/AnalogInputPins
analogWrite: https://www.arduino.cc/en/Reference/AnalogWrite
analogRead: https://www.arduino.cc/en/Reference/AnalogRead
analogReference: https://www.arduino.cc/en/Reference/AnalogReference

Remember: Always set the pin as an input or output before using it.

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 9 of 10

 
More on Analog Outputs: PWM Filtering
Using a technique called PWM filtering, it is possible to suppress the AC component of the PWM signal
with a low-pass filter extract the DC component. The PWM output of Arduino UNOs have a frequency of
about 500-1000Hz, and an appropriate low-pass filter should be used.

1
fc = 2πRC
Low-Pass Filter (Source: ​
https://en.wikipedia.org/wiki/Low-pass_filter​
)

This is the output when using a low-pass filter with R=100k​Ω​ and C=1uF (fc=1.59Hz), and an analogWrite
value of 50 (roughly 20% duty). ​Blue is the pin output​
, and ​
red is the filtered output.

The DC output voltage of a properly filtered PWM signal can be found with the following formula:

V out = V max * %duty  


50
255 * 5.0v ≈ 1.0v  

Arduino Documentation - Fall 2015 Digital and Analog I/O Page 10 of 10

Вам также может понравиться