SAMI MAKERSPACE MAKE: AN ELECTRONICS WORKSHOP
ARDUINO BASICS Credit to: Sparkfun and Linz Craig, Nick Poole, Prashanta Aryal, Theo Simpson, Tai Johnson, and Eli Santistevan
THIS PRESENTATION: Part One: Hardware Getting around and Arduino Getting connected The breadboard A simple circuit Part Two: Software The integrated development environment Part Three: Programming Inputs and outputs Some conventions First Program: Blink Variables Analog versus digital Pulse wave modulation Second Program: Fade Third Program: Button Conditional statements Serial communication
PART ONE: THE HARDWARE ARDUINO, USB CABLE, LAPTOP, BREADBOARD
GETTING AROUND AN ARDUINO THE “UNO” MODEL
Analog INPUTS Digital I\O PWM(3, 5, 6, 9, 10, 11) PWR INUSB (to Computer) SCL\SDA (I2C Bus) POWER 5V / 3.3V / GND RESET
LET’S GET CONNECTED!
PLUG IN YOUR ARDUINO
BREADBOARDS RAPID AND SOLDER-FREE PROTOTYPING!
PROTOTYPING CIRCUITS SOLDERLESS BREADBOARD One of the most useful tools in an engineer or Maker’s toolkit. The three most important things: A breadboard is easier than soldering A lot of those little holes are connected, which ones? Sometimes breadboards break
WHAT’S A BREADBOARD?
SOLDERLESS BREADBOARD Each row (horiz.) of 5 holes are connected. Vertical columns – called power bus are connected vertically
A SIMPLE CIRCUIT LIGHT UP AN LED USING THE ARDUINO
USE THE BREADBOARD AND ARDUINO TO BUILT A SIMPLE CIRCUIT Use the breadboard to wire up a single LED with a 330 Ohm Resistor (Orange- Orange-Brown). Note: the longer leg on the LED is the positive leg and the shorter leg is the negative
PART TWO: THE SOFTWARE INTEGRATED DEVELOPMENT ENVIRONMENT
THE INTEGRATED DEVELOPMENT ENVIRONMENT
OPEN UP “ARDUINO” APPLICATION Look for the icon on the desktop of the white clam-shell computers. You can also download the application for free to your own computers (you may need to also install the proper drivers)
ARDUINO: INTEGRATED DEVELOPMENT ENVIRONMENT (IDE) Two required functions / methods / routines: void setup() { // runs once } void loop() { // repeats } error & status messages
SETTINGS: TOOLS SERIAL PORT Your computer communicates to the Arduino microcontroller via a serial port through a USB-Serial adapter. Check to make sure that the drivers are properly installed.
SETTINGS: TOOLS BOARD Next, double-check that the proper board is selected under the Tools Board menu.
PART 3: PROGRAMMING! CODE FOR THE MASSES
CONCEPT: INPUTS VS. OUTPUTS
CONCEPTS: INPUT VS. OUTPUT Referenced from the perspective of the microcontroller (electrical board). Inputs is a signal / information going into the board. Output is any signal exiting the board. Almost all systems that use physical computing will have some form of output. What are some examples of Outputs?
CONCEPTS: INPUT VS. OUTPUT Inputs is a signal / information going into the board. Output is any signal exiting the board. Examples: Buttons Switches, Light Sensors, Flex Sensors, Humidity Sensors, Temperature Sensors… Examples: LEDs, DC motor, servo motor, a piezo buzzer, relay, an RGB LED
YOU DON’T NEED TO SPEAK BINARY! The Arduino IDE allows you to work in a “high-level programming language.” The code you write is readable to humans (with a little training) The IDE does the hard work of making your code something that the chip on the Arduino understands:
SUB-ROUTINES/FUNCTIONS/COMMANDS Arduino uses pre-made sub-routines (small programs) to make your life easier. Programs like: delay() if() loop() Most programs need an input (what they are acting on) and give an output (usually a numerical value) The names of these programs (like most things in Arduino) are case sensitive, so type carefully! digitalWrite() is not the same as digitalwrite() Can you guess what these do?
pinMode(pin, INPUT/OUTPUT); ex: pinMode(13, OUTPUT); digitalWrite(pin, HIGH/LOW); ex: digitalWrite(13, HIGH); delay(time_ms); ex: delay(2500); // delay of 2.5 sec. // NOTE: -> commands are CASE-sensitive
digitalWrite() analogWrite() digitalRead() if() statements / Boolean analogRead() Serial communication BIG 6 CONCEPTS
SOME CONVENTIONS: CURLY BRACKETS When you call a sub-routine every thing that happens in the program should be contained in curly brackets: Everything between the curly brackets {..} is part of the loop function.
SOME CONVENTIONS: SEMICOLONS Any line of code that isn’t calling a sub- routine, or is a sub-routine that is only one line long should always end with a semicolon (;) If you forget these, you will get errors when you try to “compile” the code!
SOME CONVENTIONS: COMMENTS It’s easier to get lost in your own code than you may think. Leave comments to yourself, especially before you do something “tricky.” They are not needed to make the program work, but can save you a lot of trouble in the long run.
FIRST PROGRAM: BLINK LET’S MAKE AN LED BLINK!
PLAN YOUR ATTACK WITH A FLOWCHART Initialize Turn LED ON Wait Turn LED OFF Wait
1. PUT IN THE “BARE MINIMUM” The “setup” function runs once and will lock in any settings you want. The “loop” function will run for as long as the Arduino has power!
2. INITIALIZE THE LED PIN AS A “OUTPUT” Use the “pinMode” command. This requires two inputs (or arguments). You must tell it what pin to act on (13 in this case) and whether that pin is an OUTPUT or INPUT (this is case sensitive).
3. TURN ON THE LED FOR ONE SECOND
4. TURN OFF THE LED FOR ONE SECOND
5. REPEAT! Once the end of the program is reached, it will go to the beginning of loop() and run it again. And again. And again….
SOME EXTRA CHALLENGES… Make the light blink with a 200 ms interval. Change the blink to mimic a heart beat. What’s the fastest blink your eye can detect? Add two, three, or four LED’s. (each LED will need its own 330 Ohm resistor).
CONCEPT: VARIABLES
Declaring variables Types of variables Variable scope Initializing variables Variable rollover
TYPES OF VARIABLES 8 bits16 bits32 bits byte char int unsigned int long unsigned long float
DECLARING VARIABLES AND SCOPE OF VARIABLES Variable Scope Global Vs. Function-level
INITIALIZING A VARIABLE: EXAMPLE Compare this code our early “blink” program. There is an “int” type variable declared. An integer (no decimal) The variable is named “ledPin” ledPin is initialized to the value “5” This is a “global” variable since it was declared outside of a function. All other functions are able to “call” ledPin and they can change its value Notice how easy it would be to change the pin on your circuit that has the LED connected to it?
CONCEPT: DIGITAL VERSUS ANALOG
CONCEPTS: ANALOG VS. DIGITAL Microcontrollers are digital devices – ON or OFF. Also called – discrete. Analog signals are anything that can be a full range of values. What are some examples? How can we get 3V from a microcontroller that can only be on (5V) or off (0V)? 5 V 0 V 5 V 0 V
CONCEPT: PWM PULSE WAVE MODULATION
CONCEPT: PULSE WAVE MODULATION To create an analog signal, the digital microcontroller uses a technique called PWM. By varying the duty cycle, we can mimic an “average” analog voltage. Pulse Width Modulation (PWM)
SECOND PROGRAM: FADE LET’S DIM THAT LED!
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 PROJECT #2 – FADING INTRODUCING A NEW COMMAND…
THIRD PROGRAM: BUTTON CONTROL THAT LED WITH DIGITAL INPUT!
USE AN EXAMPLE PROGRAM In Arduino, open up: File Examples 02.Digital Button
DIGITAL INPUT: BUTTON to Digital Pin 2
DIGITAL INPUT: BUTTON This is just like our 1 st circuit!
DIGITAL INPUT Connect digital input to your Arduino using Pins # 0 – 13 (Although pins # 0 & 1 are also used for programming) Digital Input needs a pinMode command: pinMode (pinNumber, INPUT); Make sure to use ALL CAPS for INPUT To get a digital reading: int buttonState = digitalRead (pinNumber); Digital Input values are only HIGH (On) or LOW (Off)
DIGITAL SENSORS Digital sensors are more straight forward than Analog No matter what the sensor there are only two settings: On and Off Signal is always either HIGH (On) or LOW (Off) Voltage signal for HIGH will be a little less than 5V on your Uno Voltage signal for LOW will be 0V on most systems
USING CONDITIONAL STATEMENTS IF THIS, THEN THAT!
CONDITIONAL STATEMENTS: IF()
void loop() { int buttonState = digitalRead(5); if(buttonState == LOW) {// do something } else { // do something else } CONDITIONAL STATEMENTS: IF() DIG INPUT
BOOLEAN OPERATORS Description ( ) == ( ) is equal? ( ) != ( ) is not equal? ( ) > ( ) greater than ( ) >= ( ) greater than or equal ( ) < ( ) less than ( ) <= ( ) less than or equal
SERIAL COMMUNICATION KEEPING AN EYE ON YOUR PROGRAM…
USING SERIAL COMMUNICATION Method used to transfer data between two devices. Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit. Data passes between the computer and Arduino through the USB cable. Data is transmitted as a string of zeros (‘0’) and ones (‘1’).
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
EXAMPLE void loop ( ) { Serial.print(“Hands on “) ; Serial.print(“Learning ”) ; Serial.println(“is Fun!!!”) ; } Serial.print adds to the same line. Serial.println adds to the line, then “returns” (starts a new line).
USING SERIAL COMMUNICATION TO DEBUG AND TROUBLESHOOT Let’s use serial communication to watch what is happening in our “button” program.