Vous êtes sur la page 1sur 67

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
   

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 port
12

12

Tools>COM

   

Upload to MC Uni-blinky? Uni-blinky? Whats it mean? Comment it!

  

Feeling confident? Then make Blinky! Blinky! Answer


int 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 board


int 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


Res
Br K O
5V

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

10 kk; 10 ; Pot Pot

W W

Voltage dividers Check with multimeter


int 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); }

5V
D13

+
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




Circuit
How can we make better? 5V

A0

10 k

Introducing Map() function


int 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 signals


2.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 Libraries
2.0 ms
1.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.com
standard 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.h> 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 loop
outVal = map(inVal, 0, 1023, 0, 179);

#include <Servo.h> 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 code
int ledPin = 9; void setup() { }

GND

our output signals looks like?

void loop() { for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { analogWrite(ledPin, fadeValue); delay(30); } for(int fadeValue = 255 ; 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 <file.h> <file.h> 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+1
DISPLAY i

 

IS i<50 ?

 

YES NO END

Start and end Arrow control passing Processing Input/Output Conditional or decision What does this do? Examples

Moving from Flowchart to code


START

i=i+1
DISPLAY i

IS i<50 ?

YES NO END

Whats wrong with this code?


void loop() { for (int thisPin = 0; thisPin < 8; thisPin++) { if (thisPin == 4) { break; } for (int blinkVal = 0; blinkVal < 5; blinkVal++) { digitalWrite(thisPin, HIGH); delay(500); digitalWrite(thisPin, LOW); delay(500); } } }

What changed?
void loop() { for (int thisPin = 0; thisPin < 8; thisPin++) { if (thisPin == 4) { break; } for (int blinkVal = 0; blinkVal < 5; blinkVal++) { digitalWrite(thisPin, HIGH); delay(500); digitalWrite(thisPin, LOW); delay(500); } } }

Indentation Style
  

Indent code within {} Indent = 2 spaces Close bracket in line with open line

Why do it?
   

Can see code structure Code flow easy to imagine Easy to ID {} Convention

void loop() { for (int thisPin = 0; thisPin < 8; thisPin++) { if (thisPin == 4) { break; } for (int blinkVal = 0; blinkVal < 5; blinkVal++) { digitalWrite(thisPin, HIGH); delay(500); digitalWrite(thisPin, LOW); delay(500); } } }

MC Presentation
 

Overview of problem, how you thought of it, why MC is a good solution Overview of how you solved, including


Commented + formatted code


 

cite borrowed code (important!) Be able to explain every line




Im not sure what this does = score drop

Circuit diagrams (component values and voltages where applicable)

 

Demonstration of your device What you learned, what you would do differently if you could do it again, what you would add if you had more time (and how you might do this)

Rubric (peer reviewed)


Assessment area
Commented code and explanation Circuit diagrams and explanation Demonstration: Does it show what it is supposed to? How well does it work? Overall presentation: how useful was this to listener? Perceived effort put into overall project

Score (1-10)

PRETTY BAD 1 2

NEEDS WORK 3 4

SATISFACTORY 5 6

VERY GOOD 7 8

EXCEPTIONAL 9 10

To add
  

Knock sensors Controlling DC motors




  

analogWrite() analogWrite() with transistors one way H-bridge 2 way

While loop Arrays Math (+-*/) (+Comparison ops (<,>,<=, etc) etc)


And ,or, etc multi comparators

Led calcs how to choose calcs resistors for 20 mA




 

Tone() and noTone() command noTone() Var Types


 

Red=1.7V, Y&O=2.0V, Grn=2.1V Grn=2.1V Rled=(Vin-Vled)/Imax


    

Memory size Boolean (!-not) how to define (!toggle state vars

Constrain(x,a,b) Constrain(x,a,b) Cant use pins 0+1 while uploading Time: millis() how to use millis() Functions user defined comparison (==) vs assignment (=)

homework


Take home your boards, jumper, components download what you need from http://www.arduino.cc/

Project 1


Make a light-activated lightlight (or blinky) blinky)




Light should come on when it gets dark Show me on monday

Project 2


Anything else you want (but leave Project 1 intact)

z y

V Not used D3

5V 5V
D2

Print Button Log Button


D11 D12

Your parts kits


(to keep in your labeled project boxes)
 

4 x leds (your choice) resistors


 

  

4 x 470 2 x 10k

1 x photo resistor 1 x 3-pin header (later) 3Other




(Br K O)

Foam pin-protector pin

  

2 x buttons (mom-on) (mom1 x pot (10k ) 1x Piezo speaker

For buttons and pots

    

Jumpers (5-10) (5Arduino (numbered) USB cable (numbered) Breadboard (numbered) Power supply (later)

Vous aimerez peut-être aussi