arduino intro1a- mod for line follower
TRANSCRIPT
Introduction to Microcontrollers
How to make Blinky with a MC?
MC Blinky Recipe
Hardware
Precautions
The Arduino MC
Static Chip fragility Lead breakage for components 40mA current limits!! Turn off while building circuits If power LED goes out, turn off immediately
Your parts kits(well keep adding to)
Labeled parts bin Arduino (numbered) USB cable (numbered) Breadboard (numbered) 2 x leds (your choice) resistors
2 x 470
MC Blinky Recipe
Hardware
V
The Arduino MC USB cable 2 LEDs 2 470 resistors Circuit #1 (test - no control over light) Why the resistor?
GND
Circuit
MC Blinky Recipe
Hardware
The code
The Arduino MC USB cable 2 LEDs 2 470 resistors Circuit #2 (control over 1 light)
But 1st our Program Development Environment (PDE) Run Arduino.exe
Circuit
D12
GND
Coders rule: never start from scratch Modify working code
Heres some working code (we wont worry about what it means yet) Save sketch Compile Set COM port12
12
Tools>COM
Upload to MC Uni-blinky? Uni-blinky? Whats it mean? Comment it!
Feeling confident? Then make Blinky! Blinky! Answerint RedledPin = 12; int GrnledPin = 13; void setup() { pinMode(RedledPin, OUTPUT); pinMode(GrnledPin, OUTPUT); } void loop() { digitalWrite(RedledPin, HIGH); digitalWrite(GrnledPin, LOW); delay(1000); digitalWrite(RedledPin, LOW); digitalWrite(GrnledPin, HIGH); delay(1000); }
D12 D13
What is a microcontroller?
Human analogy (3D vis)
Brain (CPU) Inputs (sensors) Outputs (actuators)
What MCs will you use today?
Why Arduino? Arduino?
Easy to use
Processing
OpenOpen-source Robust Cheap
$35 for board $4 /Atmega368 Arduino.cc
Huge user community
Getting to know your Arduino (Duemilanove) Duemilanove)
Arduino Specs
http://arduino.cc/en/Main/ ArduinoBoardDuemilanove Atmega 368
In/out pins
Power
Reset
32KB Flash, 16MHz
Reset button Power jack (9V) USB jack Lights PWR, TX, RX, L ICSP header(6 pins) direct MC programming (no bootloader) bootloader)
3V3, 5V GND Vin
Analog in - 0,1,2,3,4,5 Digital
2,4,7,8,12,13 2,4,7,8,12,13 0(RX),1(TX) PWM 3,5,6,9,10,11 GND,AREF
The PDE
Choosing board Choosing COM Saving/loading Examples Compiling Uploading
Digital Out review (Blinky) (Blinky)
ReRe-make if not on your boardint RedledPin = 12; int GrnledPin = 13; void setup() { pinMode(RedledPin, OUTPUT); pinMode(GrnledPin, OUTPUT); } void loop() { digitalWrite(RedledPin, HIGH); digitalWrite(GrnledPin, LOW); delay(1000); digitalWrite(RedledPin, LOW); digitalWrite(GrnledPin, HIGH); delay(1000); }
D12 D13
Digital in
What is digital in? Parts:
Pushbutton
1, 4 2, 3
1 2
4 3
1, 4 2, 3
1, 4 2, 3
How it works How to place it
10k
ResBr K O5V
Jumpers Why wont this work? add pull-down resistor pullExamples->DigitalExamples->Digital>Button
The circuit
D2
The code
D12 D13
const int buttonPin = 2; const int ledPin = 13; int buttonState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } 5V void loop(){ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
D2
Lets dissect the code
IF ELSE
Challenge button-activated Blinky button-
one solution Problems with? Better solutions? What would we do if our code didnt work?
const int buttonPin = 2; const int ledPin1 = 13; const int ledPin2 = 12; int buttonState = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(buttonPin, INPUT); } void loop(){ buttonState = digitalRead(buttonPin); if (buttonState == HIGH) { digitalWrite(ledPin1, HIGH); digitalWrite(ledPin2, LOW); delay(1000); digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, HIGH); delay(1000); } else { digitalWrite(ledPin1, LOW); digitalWrite(ledPin2, LOW); } }
Debugging code
How do you know whats going on in your MCs brain? Introducing the Serial monitor a brain window How do we use it?
Lets look at buttonState in the Button code
Examples->DigitalExamples->Digital>Button Add the following lines: Compile Upload Working? Now open Serial Monitor Working? What do these lines do? How is this debugging?
const int buttonPin = 2; const int ledPin = 13; int buttonState = 0; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT); } void loop(){ buttonState = digitalRead(buttonPin); Serial.print(buttonState= ); Serial.println(buttonState); if (buttonState == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
Our MC tool chest (so far)
Inputs
Serial In (from computer) Push button (digitalRead) digitalRead)
Code
Structure
Outputs
setup loop IF ELSE delay(#milliseconds) delay(#milliseconds) pinmode( pinmode(pin#, mode) mode)
Led lights (digitalWrite) (digitalWrite) Serial Out (to computer)
Commands
More Inputs!
5V
Analog input Hardware
A A
+ + +
10k
pot
A0
10 k
The circuit
W B
A
10 kk; 10 ; Pot Pot
W W
Voltage dividers Check with multimeterint sensorPin = 0; //int ledPin = 13; int sensorValue = 0; void setup() { Serial.begin(9600); // pinMode(ledPin, OUTPUT); }
B B
The code Upload, open Serial Monitor Working? Dissect code Challenge: variable rate uni-blinky (0uni(01024ms)
void loop() { sensorValue = analogRead(sensorPin); Serial.println(sensorValue); }
One answer More analog input!
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delay(sensorValue); digitalWrite(ledPin, LOW); delay(sensorValue); }
The QRD1114 reflectance sensor How does it work? How to connect why? To do write code to read values from serial monitor in real time
How about more output? The hardware The circuit The code Does it do what it should? More inputs?
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
5VD13
+A0
10 k
Easier way to make sound
Under construction Tone() command (add content) Note: output mode not needed with Tone Does pitch go same way? Why?
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { } void loop() { sensorValue = analogRead(sensorPin); tone(ledPin, sensorValue); }
sensorValue controls frequency, not period
Tone(pin, freq, dur) freq, dur)
LightLight-controlled blinky
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delay(sensorValue); digitalWrite(ledPin, LOW); delay(sensorValue); }
photoresistor
CircuitHow can we make better? 5V
A0
10 k
Introducing Map() functionint sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { //Serial.begin(9600); pinMode(ledPin, OUTPUT); }
Input range: Low High
Output range: Low High
void loop() { sensorValue = analogRead(sensorPin); //sensorValue = map(sensorValue, 580, 1000, 20, 500); //Serial.println(sensorValue); Set these Get these digitalWrite(ledPin, HIGH); according to from serial delay(sensorValue); your output monitor digitalWrite(ledPin, LOW); device delay(sensorValue); }
Using Comments // to keep code you might use later handy
Homework: make a Theremin
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
How can we make better? 5V
A0
D13
+
10 k
int sensorPin = 0; int ledPin = 13; int sensorValue = 0; void setup() { //Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { sensorValue = analogRead(sensorPin); //sensorValue = map(sensorValue, 580, 1000, 20, 1000); //Serial.println(sensorValue); digitalWrite(ledPin, HIGH); delayMicroseconds(sensorValue); digitalWrite(ledPin, LOW); delayMicroseconds(sensorValue); }
Use Serial Monitor to get 1st 2 values in map() Then comment out Serial.println command
What if Daves not around to hold my hand?
http://arduino.cc/en/Reference/HomePage http://arduino.cc/en/Reference/HomePage
Cutting the USB umbilical
And mounting Arduinos on boards
Our MC tool chest (so far)
Inputs
Serial In (from computer) Push button (digitalRead) digitalRead) Pot (analogRead) (analogRead)Photoresistor (analogRead) analogRead)
Code
Structure
setup loop IF ELSE delay(#milliseconds) delay(#milliseconds)delayMicroseconds(#microseconds) delayMicroseconds(#microseconds)
Commands
Outputs
Led lights (digitalWrite) (digitalWrite) Serial Out (to computer) Speaker (digitalWrite) (digitalWrite)
pinmode( pinmode(pin#, mode) mode) map(val,inlow,inhigh, map(val,inlow,inhigh, outlow,outhigh) outlow,outhigh)
How about some movement ?
Introducing the Servo What does it do?
How is this different from a regular DC motor?
How do you control it?
Pulse Width Modulation PWM) Almost
Analog output?
Cant encode info in V (+5 or 0V) Can encode in time
The nitty gritty of servo signals2.0 ms Vdd (5 V)standard servo www.parallax.com
2.0 ms
Vss (0 V) 20 ms
Vdd (5 V)
But you dont need to know any of this thanks to Arduino #include Libraries2.0 ms1.0 ms Vdd (5 V)standard servo www.parallax.com
1.0 ms
Vss (0 V)Vss (0 V)
10 o clock
2 o clock
12 o clock
20 ms
1.5 ms Vdd (5 V)standard servo www.parallax.com standard servo www.parallax.com standard servo www.parallax.comstandard servo www.parallax.com
1.5 ms
Vss (0 V)
1.0 ms
2.0 ms
1.5 ms
20 ms
Lets try make our servo work The Hardware
Servo 3-pin header Jumpers
#include Servo myservo; int pos = 0; void setup() { myservo.attach(9); } void loop() { for(pos = 0; pos < 180; pos += 1) { myservo.write(pos); delay(15); } for(pos = 180; pos>=1; pos-=1 { myservo.write(pos); delay(15); } }
The circuit The code: Example-> ExampleServo -> Sweep
Dont modify damage!
Working? Dissect code
5V D9
GND
Challenge: make a potpotcontrolled servo Dont cheat and use Knob code! The circuit Hints: Dont need for loopoutVal = map(inVal, 0, 1023, 0, 179);
#include 5V Servo myservo; int potpin = 0; int val; void setup() 10 k A0 { myservo.attach(9); } void loop() { val = analogRead(potpin); val = map(val, 0, 1023, 0, 179); myservo.write(val); }
Working?
An answer
5V D9
GND
Another use for PWM?
Light fading
D9
The Hardware - LED The Circuit The code: Example-> Analog -> ExampleFading Working? What would Dissect codeint ledPin = 9; void setup() { }
GND
our output signals looks like?
void loop() { for(int fadeValue = 0 ; fadeValue = 0; fadeValue -=5) { analogWrite(ledPin, fadeValue); delay(30); } }
490Hz
Our MC tool chest (so far)
Inputs
Serial In (from computer) Push button (digitalRead) digitalRead) Pot (analogRead) (analogRead)Photoresistor (analogRead) analogRead)
Code
Structure
setup loop if else for delay(#milliseconds) delay(#milliseconds)delayMicroseconds(#microseconds) delayMicroseconds(#microseconds)
Outputs
Commands
LED on/off (digitalWrite) (digitalWrite)
Serial Out (to computer) Speaker (digitalWrite) (digitalWrite) Servo (myServo.write) (myServo.write) LED fade (analogWrite) (analogWrite)
pinmode( pinmode(pin#, mode) mode) map(val,d0,d1,r0,r1) #include Servo objectname
More?
Other sensors/actuators
Input (sensors)
Piezo knock sensor Accelerometer PIR (motion detector) Temperature Pressure Strain Force Microphone GPS US sensor more
Output (actuators/ indicators)
LED and LCD displays DC motor Stepper motor Cameras Solonoids more
MC project
Goal: Make an MC device that interests you
Ideas
at least 1 output at least 1 input
Can use pre-written code preparts (cite), but overall project should be your own Level of complexity should reflect your skill level Daily progress report (oral) I may add/subtract from your project Assessment (more to come) Due early/mid Nov
Musical instrument Spooky Halloween thing Science Data Logger
Solar, weather, water quality, go-kart motion, goenergy monitoring, compost, animal camera
Timelapse camera Robot Reverse Geocache puzzle Blackcloud.org Arduino Playground
Example projects
AntiAnti-poo alarm
Bunny potty trainer
Puzzle box
Words on wheels
Solar tracker
RoboRobo-roach
Teen cave
Flowcharts
What are they?
Graphical representation of program flow Clear idea of structure Loops look like loops Easy to ID endless loops Easy to move from chart to code
Why use them?
START
Flowchart symbols
i=i+1DISPLAY i
IS i