Arduino Training Topics: Microcontrollers Arduino Schematic Explanation Programming Basics Digital Output Analog to Digital Conversion 16x2 LCD Interfacing
Based on kit from www.avmicrotech.com
Kit Content www.avmicrotech.com
LED with series resistor
Prototyping Shield Basic
Jumpers
What is a Microcontroller
www.mikroe.com/chapters/view/1
• A small computer on a single chip • containing a processor, memory, and input/output • Typically "embedded" inside some device that they control • A microcontroller is often small and low cost
What is a Development Board? A printed circuit board designed to facilitate work with a particular microcontroller.
• Typical components include: power circuit programming interface basic input; usually buttons and LEDs I/O pins
How is it made?
How is it made?
• • • •
Schamatic
PCB Layout Circuit Idea
PCB Factory
Completed PCB
PCB Assembly and Testing
Bare PCB
Functional Board
PCB CAD Files
Arduino as programmer
Arduino can be used as avrdude programmer to program other AVR uC. Some arduino boards have jumpers to configure its hardware for such applications
Arduino as programmer USB to UART
USB Jack
5V Voltage Regulator DC Jack
RESET
ATMEGA 328
The Arduino Microcontroller: Atmel ARV Atmega 328
Specification Making-robots-with-arduino.pdf
What is the Arduino
todbot.com/blog/bionicarduino
Getting Started • Check out: http://arduino.cc/en/Guide/HomePage 1. 2. 3. 4. 5. 6. 7. 8.
Download & install the Arduino environment (IDE) Connect the board to your computer via the UBS cable If needed, install the drivers (not needed in lab) Launch the Arduino IDE Select your board Select your serial port Open the blink example Upload the program
Try It: Connect the USB Cable
todbot.com/blog/bionicarduino
Arduino IDE
See: http://arduino.cc/en/Guide/Environment for more information
The Environment
Parts of the Sketch
Select Serial Port and Board
Status Messages
todbot.com/blog/bionicarduino
todbot.com/blog/bionicarduino
A Little Bit About Programming • Code is case sensitive • Statements are commands and must end with a semi-colon • Comments follow a // or begin with /* and end with */
Our First Program This is the default blink code using on board LED on pin 13. You may change it to pin 2 and use circuit on next page.
LED Connection LED
Current Limiting Resistor
Note:-LED is connected to pin 2.
Project #2 – Fading Introducing a new command… •analogWrite(pin, val);
• •pin – refers to the OUTPUT pin (limited to pins 3, 5, 6, 9, 10, 11.) – denoted by a ~ symbol •val – 8 bit value (0 – 255). • 0 => 0V | 255 => 5V
LED Fading LED
Note:-LED is connected to pin 9. Current Limiting Resistor
Move one of your LED pins over to Pin 9 • In Arduino, open up: • File Examples 01.Basics Fade
Fade - Code Review
Fade - Code Review
Project# 2 -- Fading • Challenge 2a – Change the rate of the fading in and out. There are at least two different ways to do this – can you figure them out? • Challenge 2b – Use 2 (or more) LEDs – so that one fades in as the other one fades out.
Trimpot (Potentiometer) Variable Resistor fixed end wiper fixed end
Analog Sensors 3 Pin Potentiometer = var. resistor (circuit) a.k.a. Voltage Divider Circuit
wiper fixed ends
1.0 V
1.0 V
Trimpot Internal Trimpot
Ohms Law… (just the basics) Actually, this is the “voltage divider”
analogRead() • Arduino uses a 10-bit A/D Converter: • this means that you get input values from 0 to 1023 • 0V0 • 5 V 1023
•Ex: •
int sensorValue = analogRead(A0);
Using Serial Communication Method used to transfer data between two devices.
Data passes between the computer and Arduino through the USB cable. Data is transmitted as zeros (‘0’) and ones (‘1’) sequentially.
Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit.
Serial Monitor & analogRead()
Initializes the Serial Communication
9600 baud data rate prints data to serial bus
Serial Monitor & analogRead() Opens up a Serial Terminal Window
Adjust the baudrate to your code baudrate
Analog Sensors 2 Pin Analog Sensors = var. resistor •Take two sensors -- Use the Serial Monitor and find the range of input values you get for each sensor. •MaxAnalogRead = _________ •MinAnalogRead = _________
Analog Sensors Examples: Sensors Mic Photoresistor Potentiometer Temp Sensor Flex Sensor Accelerometer
Variables soundVolume lightLevel dialPosition temperature bend tilt/acceleration
Light Sensor
NTC Temperature Sensor
Set Lamp Intensity
// Set Lamp Intensity const int digital1 = 2; // const int digital2 = 3; // const int digital3 = 4; // const int digital4 = 5; // const int pwmout = 9; int sensorValue; void setup() { // Set Inputs: pinMode(digital1,INPUT); pinMode(digital2,INPUT; pinMode(digital3,INPUT; pinMode(digital4,INPUT; } void loop() { // read the analog in value: if (digitalRead(digital1)==0){ sensorValue = 0; } if (digitalRead(digital2)==0){ sensorValue = 50; } if (digitalRead(digital3)==0){ sensorValue = 150; } if (digitalRead(digital4)==0){ sensorValue = 255; } // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(100); }
16x2 LCD Connection
If the display is blank then adjust contrast adjust trimmer
/* LiquidCrystal Library - Autoscroll Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface. This sketch demonstrates the use of the autoscroll() and noAutoscroll() functions to make new text scroll or not. The circuit: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe modified 22 Nov 2010 by Tom Igoe This example code is in the public domain. http://arduino.cc/en/Tutorial/LiquidCrystalAutoscroll */
// include the library code: #include // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16,2); } void loop() { // set the cursor to (0,0): lcd.setCursor(0, 0); // print from 0 to 9: for (int thisChar = 0; thisChar < 10; thisChar++) { lcd.print(thisChar); delay(500); } // set the cursor to (16,1): lcd.setCursor(16,1); // set the display to automatically scroll: lcd.autoscroll(); // print from 0 to 9: for (int thisChar = 0; thisChar < 10; thisChar++) { lcd.print(thisChar); delay(500); } // turn off automatic scrolling lcd.noAutoscroll(); // clear screen for the next loop: lcd.clear(); }
Electricity \ Electronics Basic Concept Review • • • • •
Ohms Law Voltage Current Resistance Using a Multi-meter
Ohm’s Law
Electrical Properties Voltage
Current
Resistance
V
I
R
• Defined as the amount of potential energy in a circuit. • Units: Volts (V)
• The rate of charge flow in a circuit. • Units: Amperes (A)
Current Flow Analogy
High Current
Low Current
Voltage Analogy Water Tower Water Tower
V
V
More Energy == Higher Voltage
Less Energy == Lower Voltage
Resistance Analogy Water Tower
Water Tower
V
Big Pipe == Lower Resistance
Small Pipe == Higher Resistance
Continuity – Is it a Circuit? The word “circuit” is derived from the circle. An Electrical Circuit must have a continuous LOOP from Power (Vcc) to Ground (GND). Continuity is important to make portions of circuits are connect. Continuity is the simplest and possibly the most important setting on your multi-meter. Sometimes we call this “ringing out” a circuit.
Measuring Electricity – Voltage Voltage is a measure of potential electrical energy. A voltage is also called a potential difference – it is measured between two points in a circuit – across a device.
Measuring Electricity -- Current Current is the measure of the rate of charge flow. For Electrical Engineers – we consider this to be the movement of electrons. In order to measure this – you must break the circuit or insert the meter in-line (series).
Measuring Electricity -- Resistance Resistance is the measure of how much opposition to current flow is in a circuit. Components should be removed entirely from the circuit to measure resistance. Note the settings on the multi-meter. Make sure that you are set for the appropriate range.
Resistance settings
Terminology
Digital I/0 www.mikroe.com/chapters/view/1
pinMode(pin, mode) Sets pin to either INPUT or OUTPUT
digitalRead(pin) Reads HIGH or LOW from a pin
digitalWrite(pin, value) Writes HIGH or LOW to a pin
Electronic stuff Output pins can provide 40 mA of current Writing HIGH to an input pin installs a 20KΩ pullup
Arduino Timing • delay(ms) – Pauses for a few milliseconds
• delayMicroseconds(us) – Pauses for a few microseconds
• More commands: arduino.cc/en/Reference/HomePage
Digital? Analog? • • • •
Digital has two values: on and off Analog has many (infinite) values Computers don’t really do analog, they quantize Remember the 6 analog input pins---here’s how they work
todbot.com/blog/bionicarduino
Bits and Bytes
Variables
www3.ntu.edu.sg
Putting It Together • Complete the sketch (program) below. • What output will be generated by this program? • What if the schematic were changed?
www.ladyada.net/learn/arduino
Good References www.arduino.cc
The Arduino Environment
Board Type
Serial Port / COM Port
The Environment
Parts of the Sketch
Comments • Comments can be anywhere
Comments • Comments can be anywhere • Comments created with // or /* and */
Comments • Comments can be anywhere • Comments created with // or /* and */ • Comments do not affect code
Comments • Comments can be anywhere • Comments created with // or /* and */ • Comments do not affect code • You may not need comments, but think about the community!
Operators The equals sign = is used to assign a value == is used to compare values
Operators –And & Or –&& is “and” –|| is “or”
Variables Basic variable types: Boolean Integer Character
Declaring Variables Boolean: boolean variableName;
Declaring Variables Boolean: boolean variableName; Integer: int variableName;
Declaring Variables Boolean: boolean variableName; Integer: int variableName; Character: char variableName;
Declaring Variables Boolean: boolean variableName; Integer: int variableName; Character: char variableName; String: stringName [ ];
Assigning Variables Boolean: variableName = true; or variableName = false;
Assigning Variables Boolean: variableName = true; or variableName = false; Integer: variableName = 32767; or variableName = -32768;
Assigning Variables –Boolean: variableName = true; –or variableName = false; –Integer: variableName = 32767; –or variableName = -32768; –Character: variableName = ‘A’; –or stringName = “SparkFun”;
Variable Scope Where you declare your variables matters
Setup void setup ( ) { }
The setup function comes before the loop function and is necessary for all Arduino sketches
Setup void setup ( ) { }
The setup header will never change, everything else that occurs in setup happens inside the curly brackets
Setup void setup ( ) { pinMode (13, OUTPUT); }
Outputs are declare in setup, this is done by using the pinMode function This particular example declares digital pin # 13 as an output, remember to use CAPS
Setup void setup ( ) { Serial.begin;}
Serial communication also begins in setup This particular example declares Serial communication at a baud rate of 9600. More on Serial later...
Setup, Internal Pullup Resistors void setup ( ) { digitalWrite (12, HIGH); }
You can also create internal pullup resistors in setup, to do so digitalWrite the pin HIGH This takes the place of the pullup resistors currently on your circuit 7 buttons
Setup, Interrupts void setup ( ) { attachInterrupt (interrupt, function, mode) } You can designate an interrupt function to Arduino pins # 2 and 3 This is a way around the linear processing of Arduino
Setup, Interrupts void setup ( ) { attachInterrupt (interrupt, function, mode) } Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2 and 3 respectively Function: the function to call when the interrupt occurs
Mode: defines when the interrupt should be triggered
Setup, Interrupts void setup ( ) { attachInterrupt (interrupt, function, mode) } •LOW whenever pin state is low •CHANGE whenever pin changes value •RISING whenever pin goes from low to high •FALLING whenever pin goes from low to high –Don’t forget to CAPITALIZE
If Statements if ( this is true ) { do this; }
If if ( this is true ) { do this; }
Conditional if ( this is true ) { do this; }
Action if ( this is true ) { do this; }
Else else { do this; }
Basic Repetition • loop • For • while
Basic Repetition void loop ( ) { }
Basic Repetition void loop ( ) { }
Basic Repetition void loop ( ) { }
The “void” in the header is what the function will return (or spit out) when it happens, in this case it returns nothing so it is void
Basic Repetition void loop ( ) { } The “loop” in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already has a name
Basic Repetition void loop ( ) { } The “( )” in the header is where you declare any variables that you are “passing” (or sending) the function, the loop function is never “passed” any variables
Basic Repetition void loop ( ) { }
Basic Repetition for (int count = 0; count