02 ArduinoUNO
02 ArduinoUNO
Features
Microcontroller: ATmega328
Operating Voltage: 5V
Input Voltage (recommended): 7-12V
Input Voltage (limits): 6-20V
Digital I/O Pins: 14 (of which 6 provide PWM output)
Analog Input Pins: 6
DC Current per I/O Pin: 40 mA
DC Current for 3.3V Pin: 50 mA
Flash Memory 32 KB (ATmega328)
of which 0.5 KB used by bootloader
SRAM: 2 KB (ATmega328)
EEPROM: 1 KB (ATmega328)
Clock Speed: 16 MHz
Digital I/O
Digital pins on an Arduino board can be used for
general purpose I/O via pinMode, digitalRead and
digitalWrite functions.
Source:
http://arduino.cc/
Program Structure
#include ...
// vars declaration
void setup() {
// initialization instructions
}
void loop() {
// main program
}
I/O: pinMode
Params:
¤ pin:
the number of the pin whose mode you wish to set.
¤ mode:
n INPUT
n OUTPUT
n INPUT_PULLUP
Returns: Nothing
I/O: digitalRead
Reads the value from a specified digital pin, either
HIGH or LOW.
Note that if the pin isn't connected to anything,
digitalRead can return either HIGH or LOW (and this
can change randomly).
I/O: digitalRead
digitalRead(pin)
Params:
¤ pin:
the number of the pin whose value you wish to
read.
Returns:
¤ HIGH
¤ LOW
I/O: digitalWrite
Write a HIGH or a LOW value to a digital pin.
Params:
¤ pin: the number of the pin whose value you wish to
write.
¤ value: the value to set HIGH or LOW.
Returns: Nothing
Example 1
// Sets pin 13 to the same value as pin 7,
// declared as an input.
void setup()
{
pinMode(ledPin, OUTPUT); // sets the pin 13 as output
pinMode(inPin, INPUT); // sets the pin 7 as input
}
Example 1
void loop()
{
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the
// button's value
}
I/O: analogRead
Reads the value from the specified analog pin. The
Arduino board contains a 6 channel (8 channels on the
Mini and Nano, 16 on the Mega), 10-bit analog to
digital converter. This means that it will map input
voltages between 0 and 5 volts into integer values
between 0 and 1023. This yields a resolution between
readings of: 5 volts / 1024 units or, .0049 volts (4.9
mV) per unit.
It takes about 100 microseconds (0.0001 s) to read an
analog input, so the maximum reading rate is about
10,000 times a second.
I/O: analogRead
analogRead(pin)
Params:
¤ pin: the number of the analog input pin to read (from
0 to 5)
Returns:
¤ int (0 to 1023)
I/O: analogWrite
Writes an analog value (PWM wave) to a pin. Can be
used to light a LED at varying brightnesses or drive a
motor at various speeds. After a call to analogWrite,
the pin will generate a steady square wave of the
specified duty cycle until the next call to analogWrite
(or a call to digitalRead or digitalWrite on the same
pin).
The frequency of the PWM signal is approximately 490
Hz.
You do not need to call pinMode to set the pin as an
output before calling analogWrite.
I/O: analogWrite
analogWrite(pin, value)
Params:
¤ pin: the number of the pin whose value you wish to
write.
¤ value: the duty cycle which is between 0 (always off)
and 255 (always on).
Returns: Nothing
I/O: analogWrite - PWM
Pulse Width Modulation, or PWM, is a technique for
getting analog results with digital means. Digital control
is used to create a square wave, a signal switched
between on and off. This on-off pattern can simulate
voltages in between full on (5 Volts) and off (0 Volts) by
changing the portion of the time the signal spends on
versus the time that the signal spends off. The duration
of "on time" is called the pulse width.
I/O: analogWrite - PWM
Params:
¤ pin: the pin on which to generate the tone.
¤ frequency: the frequency of the tone in hertz -
unsigned int.
¤ duration: (optional) the duration of the tone in
milliseconds - unsigned long
Returns: Nothing
Advanced I/O: noTone
Stops the generation of a square wave triggered by
tone. Has no effect if no tone is being generated.
Params:
¤ pin: the pin on which to stop generating the tone.
Returns: Nothing
Time: delay
Pauses the program for the amount of time (in
miliseconds) specified as parameter.
delay(ms)
Params:
¤ ms: the number of milliseconds to pause (unsigned long).
Returns: Nothing
Serial
Used for communication between the Arduino board
and a computer or other devices. All Arduino boards
have at least one serial port (also known as a UART or
USART): Serial. It communicates on digital pins 0 (RX)
and 1 (TX) as well as with the computer via USB. Thus, if
you use these functions, you cannot also use pins 0 and 1
for digital input or output.
Serial
You can use the Arduino environment's built-in serial
monitor to communicate with an Arduino board. Click the
serial monitor button in the toolbar and select the same
baud rate used in the call to begin.
Serial: Serial.begin
Sets the data rate in bits per second (baud) for serial
data transmission. For communicating with the computer,
use one of these rates: 300, 600, 1200, 2400, 4800,
9600, 14400, 19200, 28800, 38400, 57600, or
115200. You can, however, specify other rates - for
example, to communicate over pins 0 and 1 with a
component that requires a particular baud rate.
Params:
¤ speed: in bits per second (baud) - long
¤ config: (optional) sets data, parity, and stop bits. Valid
values are:
n SERIAL_5N1
n SERIAL_6N1
n SERIAL_7N1
n SERIAL_8N1 (the default)
Serial: Serial.begin
n SERIAL_5N2 n SERIAL_5O1
n SERIAL_6N2 n SERIAL_6O1
n SERIAL_7N2 n SERIAL_7O1
n SERIAL_8N2 n SERIAL_8O1
n SERIAL_5E1 n SERIAL_5O2
n SERIAL_6E1 n SERIAL_6O2
n SERIAL_7E1 n SERIAL_7O2
n SERIAL_8E1 n SERIAL_8O2
n SERIAL_5E2 Returns: Nothing
n SERIAL_6E2
n SERIAL_7E2
n SERIAL_8E2
Serial: Serial.available
Get the number of bytes (characters) available for
reading from the serial port. This is data that's already
arrived and stored in the serial receive buffer (which
holds 64 bytes).
Serial.available()
Params: None
Returns: The number of bytes available to read
Serial: Serial.read
Reads incoming serial data.
Serial.read()
Params: None
Returns: The first byte of incoming serial data available
(or -1 if no data is available) – int
Serial: Serial.write
Writes binary data to the serial port. This data is sent
as a byte or series of bytes. To send the characters
representing the digits of a number use the print
function instead.
Serial: Serial.write
Serial.write(val)
Serial.write(str)
Serial.write(buf, len)
Params:
¤ val: a value to send as a single byte
¤ str: a string to send as a series of bytes
Params:
¤ val:the value to print - any data type.
¤ format: specifies the number base (for integral data
types) or number of decimal places (for floating point
types).
Returns:
¤ the number of bytes written - size_t (long).
Serial: Serial.println
Prints data to the serial port as human-readable ASCII
text followed by a carriage return character (ASCII 13,
or '\r') and a newline character (ASCII 10, or '\n'). This
command takes the same forms as Serial.print.
Serial: Serial.println
Serial.println(val)
Serial.println(val, format)
Params:
¤ val:the value to print - any data type.
¤ format: specifies the number base (for integral data
types) or number of decimal places (for floating point
types).
Returns:
¤ the number of bytes written - size_t (long).
SoftwareSerial
The Arduino hardware has built-in support for serial
communication on pins 0 and 1 (which also goes to the
computer via the USB connection). The native serial
support happens via a piece of hardware (built into the
chip) called a UART. This hardware allows the Atmega
chip to receive serial communication even while working
on other tasks, as long as there room in the 64 byte
serial buffer.
SoftwareSerial
The SoftwareSerial library has been developed to
allow serial communication on other digital pins of the
Arduino, using software to replicate the functionality
(hence the name "SoftwareSerial"). It is possible to have
multiple software serial ports with speeds up to 115200
bps. A parameter enables inverted signaling for devices
which require that protocol.
SoftwareSerial
The SoftwareSerial library has been developed to
allow serial communication on other digital pins of the
Arduino, using software to replicate the functionality
(hence the name "SoftwareSerial"). It is possible to have
multiple software serial ports with speeds up to 115200
bps. A parameter enables inverted signaling for devices
which require that protocol.
The library has some known limitations for example
when using multiple software serial ports, only one can
receive data at a time.
Example 4
#include<SoftwareSerial.h>
const int rx = 3;
const int tx = 2;
const int led = 13; // Built-in Led
SoftwareSerial bt(rx, tx);
void setup() {
pinMode(led, OUTPUT);
bt.begin(115200);
Serial.begin(9600);
}
Example 4
void loop() {
if (bt.available()) {
char c = (char)bt.read();
Serial.println(c);
if (c=='1') {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}
}
Example 5
#include<SoftwareSerial.h>
const int rx = 3;
const int tx = 2;
const int led = 11; // Pin 11 supports PWM
SoftwareSerial bt(rx, tx);
void setup() {
pinMode(led, OUTPUT);
bt.begin(115200);
Serial.begin(9600);
}
Example 5
void loop() {
if (bt.available()) {
int i = bt.read();
Serial.println(i);
analogWrite(led, (i - 48) * 10);
}
}
Interrupts: attachInterrupt
Specifies a function to call when an external interrupt
occurs. Replaces any previous function that was attached
to the interrupt. Most Arduino boards (as Arduino UNO)
have two external interrupts: numbers 0 (on digital pin
2) and 1 (on digital pin 3).
Interrupts: attachInterrupt
attachInterrupt(interrupt, function, mode)
Params:
¤ interrupt can have values:
n 0 to consider interrupts on pin 2
n 1 to consider interrupts on pin 3.
Returns: Nothing
Interrupts: detachInterrupt
Turns off the given interrupt.
detachInterrupt(interrupt)
Params:
¤ Interrupt: the number of the interrupt to disable.