AICCM Arduino
AICCM Arduino
An Introduction to Arduino
PDF GUIDE: http://antoinettejcitizen.com/files/AICCM_Arduino.pdf
Desktop computers are “general purpose computers” that can run any of thousands of programs.
Microcontrollers are “special purpose computers.” Microcontrollers do one thing well.
Microcontrollers are often embedded onto a single printed circuit board. This board provides all
of the circuitry necessary for a useful control task. The intention is that the board is immediately
useful to an application developer, without them needing to spend time and effort in developing the
controller hardware.
BASIC Stamp
Microcontroller:
• Microchip Technology PIC
• Parallax SX processor.
mbed
Microcontroller:
• ARM Cortex
IOIO OTG
Microcontroller:
• Microchip Technology PIC
Arduino
Microcontroller:
Atmel AVR microcontroller
• ATmega8
• ATmega168
• ATmega328
• ATmega1280
• ATmega2560
Arduino
Single board microcontrollers like the Arduino are able to read inputs - light on a sensor, a finger on a
button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing
something online. Arduino is an open-source prototyping platform based on easy-to-use hardware and
software.
Examples: http://www.creativeapplications.net/category/arduino-2/
There are lots of different types of Arduino boards. UNO is currently the most common and easily the best
documented and best supported of the Arduinos.
Arduino Leonardo
ATmega32U4 7-12V 5V 16MHz 20* 12 7 32Kb
Arduino Pro
ATmega328 3.35 -12V 3.3V 8MHz 14 6 6 32Kb
5V
Arduino Yun
ATmega32u4 5V 5V 16MHz 20 12 7 32Kb
Atheros AR9331 3.3V 16MB
Arduino Mega
ATmega2560 7-12V 5V 16MHz 54 16 14 256Kb
Arduino Fio
ATmega328P 3.35 -12V 3.3V 8MHz 14 8 6 32Kb
Arduino-compatible
DF Robot Freetronics
ArduPhone
Barebones
Cheapduino
Lillypad
Single-board Computers
Single board computers like the Raspberry Pi use application processors rather than
microcontrollers. These boards operated more like most computers you would normally use - you
can plug in a display, keyboard and mouse, and run an operating system and applications.
int LED = 7;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
Now try changing the delay number to make LED blink faster or slower.
Resistor colour code
arduino intergrated development enviroment (ide)
1. Verify: Compiles and approves your code. It will catch errors in syntax (like missing semicolons
or parentheses).
2. Upload: Sends your code to the 101 board.
3. New: This buttons opens up a new code window tab.
4. Open: This button will let you open up an existing sketch.
5. Save: This saves the currently active sketch.
6. Serial Monitor: This will open a window that displays any serial information your 101 board is
transmitting. It is very useful for debugging.
7. Sketch Name: This shows the name of the sketch you are currently working on.
8. Code Area: This is the area where you compose the code for your sketch.
9. Message Area: This is where the IDE tells you if there were any errors in your code.
10. Text Console: The text console shows complete error messages. When debugging, the text
console is very useful.
11. Board and Serial Port: Shows you what board and the serial port selections.
Select Your Board: Arduino/Genuino UNO
Before we can start jumping into the experiments, there are a few adjustments we need to make.
This step is required to tell the Arduino IDE which of the many Arduino boards we have. Go up to
the Tools menu. Then hover over Board and make sure Arduino/Genuino UNO is selected.
Next up we need to tell the Arduino IDE to which of our computer’s serial ports the 101 is
connected. Again,, go up to Tools, hover over Port, and select your 101’s serial port. This will
have Arduino 101 next to the port number in parentheses.
Window Users: This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for
hardware serial ports). If there are multiple COM ports available, the UNO is likely the highest
numbered port in the list. To be certain, you can also disconnect your UNO and reopen the
menu; the entry that disappears should be the UNO. Reconnect the board and select that serial
port.
Mac Users: Select the serial device of the UNO from the Tools, then hover over Port. On the
Mac, this should be something with /dev/tty.usbmodem or /dev/tty.usbserial in it.
Multiple BlinKs
We will edit the previous sketch to add two more LED variables.
void setup() {
pinMode(LED, OUTPUT);
Add pinMode for the two
pinMode(LED2, OUTPUT);
added LEDs.
pinMode(LED3, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
Add digitalwrite HIGH and LOW
sequence for all LEDs delay(1000);
digitalWrite(LED, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
delay(1000);
}
fade
We use anaologWrite(pin number,0-255) rather than digitalWrite(pin number,HIGH/LOW)
in order to change the brightness of the LED. 0 being off and 255 being the brightess.
AnalogWrite only works with PWM pins - on the Arduino UNO PWM are pins 3,5,6,9,10,11.
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
Audio Shield
Motor Shield
Proto Shield
Library Manager
Libraries are a collection of code that makes it easy for you to connect to a sensor, display,
module, etc. For example, the built-in LiquidCrystal library makes it easy to talk to character
LCD displays. There are hundreds of additional libraries available on the Internet for download.
To install a new library into your Arduino IDE you can use the Library Manager (available from
IDE version 1.6.2). Open the IDE and click to the “Sketch” menu and then Include Library >
Manage Libraries.
You can also manually install libraries that are not available in the manager by copying the
library folder into Documents > Arduino > libraries
servo
Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow
the shaft to be positioned at various angles, usually between 0 and 180 degrees. As the servo
motor already contains a driver circuit, it is easy to control with an Arduino - no other circuitry
required!
Applications:
• Robotics
• Animatronics
• Radio Control Cars/Boats/Planes
Advantages:
• Low cost
• Variety - There is a wide range of sizes and torque
ratings
• Simple to control using Arduino
• Up to 12 servos can be connecting using the
Arduino servo library
Limitations:
• Limited range of motion - Most servos are limited to
180 degrees of motion.
• Moderate precision - Positioning accuracy and repeatability of +/- 1 degree is typical.
• Jitter - The feedback mechanism in the servo will actively try to correct any drift from the target
position. This constant adjustment can create annoying twitches while trying to hold a steady
position. If this is a problem for your application, consider a stepper motor instead.
Some interesting projects using servos:
Connecting to Arduino:
The power wire is typically red, and should be connected to the 5V pin
on the Arduino board.
Ground (GND)
180° Power (5V)
Signal (digital pin)
Controlling a servo:
The sketch below is an example from the Arduino IDE. EXAMPLES > SERVO > SWEEP
This code uses the Servo library, you can learn more about this library here -
https://www.arduino.cc/en/Reference/Servo
This sketch will “sweep” the servo motor by going through every position (0 - 180) and back
again with 15 millisecond delay between each position.
#include <Servo.h>
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
void loop() {
myservo.write(0); // tell servo to go to position ‘0’
delay(500); // wait 500ms
myservo.write(100); // tell servo to go to position ‘100’
delay(500); // wait 500ms
}
Instead of sweeping through every position as per the previous example, the above code will
make the servo go from position 0 to position 100 as fast as it can. A longer delay is required as
the servo needs time to get to its position before the next position signal is sent; otherwise the
new position will overwrite the previously sent position and it servo will behave erratically.
Controlling a servo with a potentiometer:
A potentiometer is a simple knob that provides a variable resistance, which we can read into
the Arduino board as an analog value. By turning the shaft of the potentiometer, we change
the amount of resistance on either side of the wiper which is connected to the center pin of the
potentiometer. This changes the relative “closeness” of that pin to 5
volts and ground, giving us a different analog input. When the shaft
is turned all the way in one direction, there are 0 volts going to the
pin, and we read 0. When the shaft is turned all the way in the other
direction, there are 5 volts going to the pin and we read 1023. In
between, analogRead() returns a number between 0 and 1023 that is
proportional to the amount of voltage being applied to the pin.
The sketch below is an example from the Arduino IDE. EXAMPLES > SERVO > KNOB
In this sketch we use a 10K potentiometer to control the position of the servo. The analog input
will return a value from 0 - 1023, and the servo needs a position of 0 -180. We can use the map()
function to scale the value from 0 - 1023 to 0 - 180, and send this value to the servo.
The map function takes data like this: map(valueToMap, fromLow, fromHigh, toLow, toHigh)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer
val = map(val, 0, 1023, 0, 180); // map pot value to servo value
myservo.write(val); // sets the servo position
delay(15); // waits for the servo to get there
}
Using multiple servos with external power:
5V
Servos draw considerable power, so if you need to drive more than one or two, you’ll probably
need to power them from a separate supply (i.e. not the +5V pin on your Arduino). The above
circuit digram examples how to connect multiple servos to external power.
You can use a 1 amp 5V DC power pack to drive about 5 servos or 10 servos with a 2 amp
power pack. Whenever you connect external power remember you always need to connect the
grounds of the Arduino and external power supply together!
Upload the sketch below to make the two servos rotate in opposite directions.
#include <Servo.h>
Servo myservo; // 1st servo
Servo myservo2; // 2nd servo
void setup() {
myservo.attach(9); // attach 1st servo
myservo2.attach(8); // attach 2nd servo
}
void loop() {
myservo.write(0); // 1st servo to position 0
myservo2.write(180); // 2nd servo to position 180
delay(500);
myservo.write(180); // 1st servo to position 180
myservo2.write(0); // 2nd servo to position 0
delay(500);
}
buzzer + photoresistor
Photoresitor measures ambient light . The light changes the resistance like a potentiometer.
This sketch will change the buzzer tone in response to the reading of the light.
int lightLevel;
int piezo = 8;
int duration = 300;
void setup()
{
pinMode(piezo, OUTPUT);
}
void loop()
{
lightLevel = analogRead(A0);
tone(piezo, lightLevel, duration);
delay(duration);
}
essential information
Type Description Notes
Soft copy of the Program code that can be compiled in File format:
Arduino Sketch Arduino IDE. .ino
.pde (format used prior to IDE 1.0)
Arduino board type What is the board? Datasheet in PDF format are
What version? often available online.
What is the arduino-compatible type?
#include <library_name.h>
#include “library_name.h”
Arduino IDE version Which version was the Arduino sketch eg. Arduino IDE 1.6.13
compiled and uploaded from?
Changes in the IDE versions
can result in a sketch being
unable to complie.
Overseas:
• https://www.adafruit.com/
• https://www.sparkfun.com/
• https://www.seeedstudio.com/