![Page 1: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/1.jpg)
Microcontroller Programming Beginning
with ArduinoCharlie Mooney
![Page 2: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/2.jpg)
Microcontrollers
Tiny, selfcontained computers in an IC Often contain peripherals Different packages availible Vast array of size and power availible
![Page 3: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/3.jpg)
![Page 4: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/4.jpg)
![Page 5: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/5.jpg)
![Page 6: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/6.jpg)
Sensory Input
Robots need to be able to recieve input from the world in the form of sensory input.
Microcontrollers handle this input. Thousands of sophisticated sensors availiable
![Page 7: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/7.jpg)
Pressure/Force Sensors
![Page 8: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/8.jpg)
GPS Locators
![Page 9: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/9.jpg)
Gyroscopes
![Page 10: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/10.jpg)
Wheel Encoders
![Page 11: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/11.jpg)
Infared Proximity Detectors
![Page 12: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/12.jpg)
Accelerometers
![Page 13: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/13.jpg)
Ultrasonic Rangefinders
![Page 14: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/14.jpg)
Alcohol Vapor Density Detectors
![Page 15: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/15.jpg)
Arduino
Development board for the ATMega328 Inludes
Programmer, Voltage Regulators Seral to USB Converter
CHEAP $30! Has everything you need!
![Page 16: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/16.jpg)
Arduino C Template
void setup() {
// Setup stuff to only run once at the beginning
}
void loop()
{
// This function gets called indefinatly
}
![Page 17: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/17.jpg)
Peripherals
Analog to Digital Converters (ADC) Counters/Timers (TMRx) PWM Modules (CCP/PWM) Serial Ports (UART) Many, many more....
![Page 18: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/18.jpg)
Digital I/O
Only HIGH and LOW values Each pin configurable to do input or output
pinMode(pinNumber, pinState)
pinMode(13, INPUT) pinMode(13, OUTPUT)
![Page 19: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/19.jpg)
Digital I/O (Part II)
Output digitalWrite(pinNumber, HIGH/LOW)
Input int val = digitalRead(pinNumber)
![Page 20: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/20.jpg)
Arduino Digital I/O Example
int ledPin = 13;
void setup() {
// Set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// Bring the pin high (1)
digitalWrite(ledPin, HIGH);
}
![Page 21: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/21.jpg)
Serial Interface (UART)
Communicate with other microcontrollers or PC's Asynch. communication Arduino libraries make it extremely easy
Serial.begin(baudRate) Serial.println(”String To Send”) int bytesWaiting = Serial.Availible() Char incomingData = Serial.read()
![Page 22: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/22.jpg)
Arduino Serial Example
void setup() {Serial.begin(9600); // Setup baud rate
}
void loop() {Serial.println(”Give me input”); // output datawhile(Serial.availible() < 1) { // if there's data waiting
char input = Serial.read(); // get a byte of data}
}
![Page 23: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/23.jpg)
Analog to Digital Converter (ADC)
Take analog voltage as input on one of the pins
Return digital representation to program
Different numbers of bits change precision.
![Page 24: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/24.jpg)
![Page 25: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/25.jpg)
![Page 26: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/26.jpg)
Light Sensors
Photoresistors Extremely Simple to Use Resistance changes with light Measure voltage over the sensor with
an ADC, and you're done
Many more complicated sensorssimulate this behavior for simplicity
![Page 27: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/27.jpg)
Arduino ADC Example
int sensorPin = 0;
void setup() { Serial.begin(9600); // Turn on Serial Connection}void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // Print sensor value to the Serial Serial.println(sensorValue);}
![Page 28: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/28.jpg)
PWM Modules (CCP)
Create PWM signals on output pins Measure PWM signals on input pins CCP stands for Capture/Compare
What is PWM, anyway?
![Page 29: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/29.jpg)
Pulse Width Modulation (PWM)
Transmit analog values using a single digital input/output pin through careful timing.
A PWM signal consists of two values Period: how long before the signal repeats Pulse Width: how long the signal is HIGH before it
goes LOW.
Duty Cycle: % of time the signal is HIGH, or(Pulse Width / Period)
![Page 30: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/30.jpg)
![Page 31: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/31.jpg)
PWM In Robotics
The average voltage (Duty Cycle * Voltage) can be used to control the speed of DC motors.
Innaccurate, poor strength, braking, and other problems exist.
Servo Motors and Speed Controllers.
![Page 32: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/32.jpg)
Servo Motors
DC Motor with gears allow for high torque Embedded microcontroller monitors PWM input
and motor position.
Vary pulse width to change position of motor
![Page 33: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/33.jpg)
Speed Controllers
Embedded microcontroller varies voltage on output lines based on PWM input.
Results in constant voltage to motors rather than intermittent.
Allow a second, more powerful, power supply to drive large motors.
Alter pusle width to change the speed of the motor
![Page 34: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/34.jpg)
Arduino PWM Command
AnalogWrite(Pin, DutyCycle) DutyCycle = 0 0%, 127 50%, 255 100%→ → → Pin can be 3, 5, 6, 9, 10, or 11
Frequency of about 490Htz Other periods are possible, but not with AnalogWrite
![Page 35: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/35.jpg)
Arduino PWM Example
int Pin = 9;
void setup()
{
pinMode(Pin, OUTPUT);
}
void loop()
{
analogWrite(Pin, 127); // Generate 50% duty cycle on ”Pin”
}
![Page 36: Microcontroller Programming Beginning with Arduino](https://reader034.vdocuments.net/reader034/viewer/2022042513/568cacd51a28ab186da922db/html5/thumbnails/36.jpg)
Useful Resources
Robot Parts and Excellent Forumswww.TrossenRobotics.com
Electrical parts, sensors, and microcontrollerswww.Sparkfun.com
Arduino Development Platformwww.ardiono.cc