Lab 01 Arduino 程式設計實驗 Essential Arduino Programming and Digital Signal Process
Arduino Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for engineering, prototype designer, artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. 2
Arduino Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. The microcontroller on the board is programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). http://www.arduino.cc/ 3
Arduino Arduino projects can be stand-alone or they can communicate with software on running on a computer (e.g. Flash, Processing, MaxMSP). http://www.arduino.cc/ 4
Arduino resources Download the latest version from the page: http://arduino.cc/en/main/software Taiwan Arduino website: http://arduino.tw/ 5
What is Arduino? The word Arduino can mean 3 things A physical piece of hardware A programming environment A community & philosophy 6
Arduino Philosophy & Community Open Source Physical Computing Platform open source hardware open source: free to inspect & modify physical computing. what? ubiquitous computing, pervasive computing, ambient intelligence, calm computing, smart objects... Community-built Examples wiki (the playground ) editable by anyone Forums with lots of helpful people 7
Arduino Hardware Similar to 8051 (if you know of it) but cheaper, faster, & open Uses AVR ATmega168/328 microcontroller chip chip was designed to be used with C language 8
LilyPad (for clothing) Arduino Hardware Variety USB DIY Boarduino Kit Bluetooth Stamp -sized many different variations to suite your needs 9
Arduino Capabilities 16 kbytes of Flash program memory 1 kbyte of RAM 16 MHz (Apple II: 1 MHz) Inputs and Outputs 13 digital input/output pins 5 analog input pins 6 analog output pins* Completely stand-alone: doesn t need a computer once programmed 10
test LED on pin 13 Arduino Diecimila Board digital input/output pins power LED USB interface TX/RX LEDs ATmega168 2 reset button 2.7 analog input pins 11
Arduino Terminology sketch a program you write to run on an Arduino board pin an input or output connected to something.»e.g. output to an LED, input from a knob. digital value is either HIGH or LOW.»(aka on/off, one/zero) e.g. switch state analog value ranges, usually from 0-255.»e.g. LED brightness, motor speed, etc. 12
Basics In the Arduino environment programs are referred to as sketches http://www.arduino.cc/ http://arduino.cc/en/reference/homepage http://arduino.cc/en/tutorial/foundations 13
Basics Comments /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We use pin 13 because, * depending on your Arduino board, it has either a builtin LED * or a built-in resistor so that you need only an LED. * * http://www.arduino.cc/en/tutorial/blink */ // LED connected to digital pin 13 14
Digital / Analog 15
Power The Arduino Duemilanove can be powered via the USB connection or with an external power supply. The power source is selected automatically. The Arduino Diecimila requires the user to select the power option with a jumper External (non-usb) power can come either from an AC-to- DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1 mm center-positive plug into the board's power jack. Leads from a battery can be inserted in the Gnd and Vin pin headers of the POWER connector 16
Power The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7 V, however, the 5 V pin may supply less than five volts and the board may be unstable. If using more than 12 V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts. 17
18 Arduino Microcontroller Boards The power pins are as follows: Vin. The input voltage to the Arduino board when it's using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin. 5V. The regulated power supply used to power the microcontroller and other components on the board. This can come either from Vin via an on-board regulator, or be supplied by USB or another regulated 5V supply. 3V3. A 3.3 volt supply generated by the on-board FTDI chip. Maximum current draw is 50 ma. GND. Ground pins. USB connector Power connector 3V3 output 5V output Vin 18
Arduino Microcontroller Boards 19 Microcontroller ATmega328 Operating Voltage 5 V Input Voltage (recommended) 7-12 V Input Voltage (limits) 6-20 V Digital I/O Pins 14 (of which 6 provide PWM output) Analog Input Pins 6 DC Current per I/O Pin 40 ma DC Current for 3.3V Pin 50 ma Flash Memory 32 KB (ATmega328) of which 2 KB used by bootloader SRAM 2 KB (ATmega328) EEPROM 1 KB (ATmega328) Clock Speed 16 MHz 19
Arduino Software Like a text editor View/write/edit sketches But then you program them into hardware 20
Installing Arduino The Steps 1. Get the Arduino software & unzip it 2. Plug in Arduino board 3. Install the driver 4. Reboot 5. Run the Arduino program 6. Tell Arduino (program) about Arduino (board) 21
Getting and Unpacking On the thumbdrives arduino-0022-win.zip for Windows On Windows, right-click Use Extract All... Find the drivers directory inside 22
Plug in Arduino board quick blink from test LED Power LED should stay on 23
Windows Driver Install 24
Selecting Location & Type usually highest-numbered port pick Diecimila 25
Arduino Software compile (verify) upload to board status area 26
Write your sketch Using Arduino Press Compile button (to check for errors) Press Upload button to program Arduino board with your sketch compile Try it out with the Blink sketch! Load File/Sketchbook/Examples/Digital/Blink upload TX/RX flash sketch runs 27
Status Messages Size depends on complexity of your sketch Uploading worked Wrong serial port selected Wrong board selected nerdy cryptic error messages 28
Troubleshooting Most common problem is incorrect serial port setting If you ever have any weird errors from the Arduino environment, just try again. The red text at the bottom is debugging output in case there may be a problem Status area shows summary of what s wrong 29
I made an LED blink, so what? Most actuators are switched on and off with a digital output The digitalwrite() command is the software portion of being able to control just about anything LEDs are easy, motors come in a bit Arduino has up to 13 digital outputs, and you easily can add more with helper chips 30
Development Cycle Make as many changes as you want Not like most web programming: edit run Edit compile upload run edit compile upload run done! 31
Lots of Built-in Examples And more here: http://www.arduino.cc/en/tutorial/homepage 32
Arduino Language Language is standard C (but made easy) Lots of useful functions pinmode() set a pin as input or output digitalwrite() set a digital pin high/low digitalread() read a digital pin s state analogread() read an analog pin analogwrite() write an analog value delay() wait an amount of time millis() get the current time And many others. And libraries add more. 33
Sketch structure Declare variables at top Initialize setup() run once at beginning, set pins Running loop() run repeatedly, after setup() 34
Basic Programming /* * Hello World! * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup() { } Serial.begin(9600); // run once, when the sketch starts // set up Serial library at 9600 bps Serial.println("Is anybody out there?"); // prints phrase with void loop() { // do nothing! } //ending line break // run over and over again // After sending program to the Arduino, press Reset button on the board and watch Serial monitor 35
Basic Programming The pinmode() function configures a pin as either an input or an output. To use it: You pass it the number of the pin to configure and the constant INPUT or OUTPUT. pinmode(11, INPUT); pinmode(13, OUTPUT); When configured as an input, a pin can detect the state of a sensor like a pushbutton. As an output, it can drive an actuator like an LED. 36
Basic Programming The digitalwrite() functions outputs a value on a pin. Possible values are: LOW (0 V)or HIGH (5 V) For example: digitalwrite(13, HIGH); digitalwrite(11, LOW); 37
Basic Programming The delay() causes the Arduino to wait for the specified number of milliseconds before continuing on to the next line. There are 1000 milliseconds in a second, so the line: delay(1000); creates a delay of one second. 38
http://arduino.cc/en/tutorial/blink // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pinmode(13, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalwrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalwrite(13, LOW); // set the LED off delay(1000); // wait for a second } 39
Variables You can use variables in a similar way as they are used in math or physics All variables have to be declared before they are used, and optionally, set an initial value (initializing the variable). 40
int Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1). Int's store negative numbers with a technique called 2's complement math. The highest bit, sometimes refered to as the "sign" bit, flags the number as a negative number. The rest of the bits are inverted and 1 is added. 41
int Example int ledpin = 13; Syntax int var = val; Syntax int var; // value will be assign later 42
Example : Blink This example shows the simplest thing you can do with an Arduino to see physical output: it blinks an LED. Hardware Required Arduino Board LED 43
Example : Blink Circuit To build the circuit, get an LED and attach its long, positive leg (called the anode) to pin 13. Attach the short negative leg (called the anode) to ground. Then plug your Arduino board into your computer, start the Arduino program, and enter the code below. 44
Explain to the Blink Code In the program below, the first thing you do is to initialize pin 13 as an output pin with the line pinmode(13, OUTPUT); In the main loop, you turn the LED on with the line: digitalwrite(13, HIGH); This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line: digitalwrite(13, LOW); That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the Arduino to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time. Once you've understood the basic examples, check out the BlinkWithoutDelay example to learn how to create a delay while doing other things. 45
Blink example code /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinmode(13, OUTPUT); } void loop() { digitalwrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalwrite(13, LOW); // set the LED off delay(1000); // wait for a second } 46
char A data type that takes up 1 byte of memory that stores a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC"). Characters are stored as numbers however. You can see the specific encoding in the ASCII chart. 47
ASCII Table 48
char The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type. Example char mychar = 'A'; char mychar = 65; // both are equivalent 49
char It is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers. 50
byte A byte stores an 8-bit unsigned number, from 0 to 255. Example byte b = B10010; // "B" is the binary formatter (B10010 = 18 decimal) 51
unsigned int Unsigned ints (unsigned integers) are the same as ints in that they store a 2 byte value. Instead of storing negative numbers however they only store positive values, yielding a useful range of 0 to 65,535 (2^16) - 1). 52
unsigned int Example unsigned int ledpin = 13; Syntax unsigned int var = val; var - your unsigned int variable name val - the value you assign to that variable 53
long long long variables are extended size variables for number storage, and store 32 bits (4 bytes), from 2,147,483,648 to 2,147,483,647. unsigned long unsigned long variables are extended size variables for number storage, and store 32 bits (4 bytes). Unlike standard longs unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 54
float / double 7. float datatype for floating-point numbers, a number that has a decimal point. They are often used to approximate analog and continuous values because they have greater resolution than integers. Floating-point numbers can be as large as 3.4028235E+38 and as low as 3.4028235E+38. They are stored as 32 bits (4 bytes) of information. 8. double double precision floating point number. Occupies 4 bytes. The double implementation on the Arduino is currently exactly the same as the float, with no gain in precision. 55
Lots of useful functions Functions pinmode() set a pin as input or output digitalwrite() set a digital pin high/low digitalread() read a digital pin s state analogread() read an analog pin analogwrite() write an analog PWM value delay() wait an amount of time (ms) millis() get the current time 56
The classroom exercise Build the traffic light control the Visual BreadBoard 4.01 (VBB) or in your real Arduino Board 57