roboticsgateway.org @roboticsgatewayFacebook.com/roboticsgateway
Introduction to Arduino
roboticsgateway.org
Setup
– Arduino IDE Downloadhttps://www.arduino.cc/en/Main/Software
2
roboticsgateway.org
What is Arduino?
• Arduino is an open-source prototyping platform based on easy to use hardware and software
• There are many different types of Arduino of different capabilities– Uno, Pro, Gemma, Mega...
3
roboticsgateway.org
Circuits
4
roboticsgateway.org
Terminology
5
roboticsgateway.org
Terminology-Hardware
Output pin - Provides current to the circuit, if the programming asks for it
Input pin - Reads whether the circuit is providing HIGH or LOW current
3.3V/5V - A special type of pin that always provides current, regardless of programming
Ground(GND) - Where the current will flow back into the board after it’s done with the circuit
6
roboticsgateway.org
Terminology-Software
Setup- This part of the code only runs ONCE. This is where you can define variables and designate pins as input or output
Loop- This part of the code runs repeatedly, forever, until your program is stopped
7
roboticsgateway.org
Breadboards
8
roboticsgateway.org
• Make a basic circuit with a blinking LED
• Learn how to program pins on the Arduino
Example 1: Blinking an LED
9
roboticsgateway.org
Schematic
10
roboticsgateway.org
pinMode(13, OUTPUT);
• Sets pin 13 as an output pin• Put this in the setup
11
roboticsgateway.org
digitalWrite(13, HIGH);
• Sets pin 13 to provide high current (turns on LED)
• Put this in your loop
12
roboticsgateway.org
delay(1000);
• Delays the program (pauses it) for 1000 milliseconds (1 second)
• Put this in your loop
13
roboticsgateway.org
digitalWrite(13, LOW);
• Sets pin 13 to provide no current (turns off LED)
• Put this in your loop
14
roboticsgateway.org
void setup() {pinMode(13, OUTPUT);
}
void loop() {digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000);
}
15
roboticsgateway.org
Potentiometer
• A potentiometer is also known as a variable resistor
• Able to return a voltage between 0 and 5 volts from its middle pin – depending on the position of the
knob
16
roboticsgateway.org
Digital Vs. Analog
• Digital– Only two states, HIGH(5V) or LOW(0V)
• Analog– Translates an input voltage to a
range of values from 0-1023• 0V at 0 and 5V at 1023
17
roboticsgateway.org
Variables
• Variable - a memory location with a name and type that stores a value
• int num; -> Declaration (request to set aside a new variable with a given name and type)
num = 3; -> Assignment Statement (assign a value to the variable)
• int num = 3; -> you can combine both steps into 1 statement
18
roboticsgateway.org
Schematic
19
roboticsgateway.org
Blinking LEDs with Potentiometer
– Blinking
• Use the potentiometer readings to
increase or decrease the gap between the next time you turn on the LED
– Commands• analogRead(sensorPin);
– reads the value (0-1023) of the analog pin
given the sensor pin
20
roboticsgateway.org
Blinking LED w/ Potentiometer Code
21
roboticsgateway.org
Buttons
• Simplest way to receive user input– When button is pushed voltage goes to
ground to arduino reads as LOW
– Use the 10K pullup resistor to reduce amount of false positive button presses
22
roboticsgateway.org
If/Else Statements
if(condition){//do something
}else if(condition 2){
//do something else}else{
//so some third thing}
23
roboticsgateway.org
If/Else Statements
Comparators: ==, !=, &&, ||, !
24
roboticsgateway.org
On/Off Buttons
• Let’s make a program where one button turns on an LED and another turns it off
• Commands– pinMode(buttonPin, INPUT);– digitalRead(buttonPin);
• read the voltage present at the pin, either HIGH(5V) or LOW(0V)
– conditionals
25
roboticsgateway.org
Schematic
26
roboticsgateway.org
On/Off Buttons Code
27
roboticsgateway.org
Challenge: 2 LEDs
• Create a program that turns an led on when it’s respective button is pressed– You need 2 LED’s controlled by 1
button each
28
roboticsgateway.org
2 LEDs Solution Codeint button1Pin = 0;int button2Pin = 1;int led1Pin = 13;int led2Pin = 12;void setup() { // put your setup code here, to run once: pinMode(button1Pin, INPUT); pinMode(button2Pin, INPUT); pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT);}void loop() { // put your main code here, to run repeatedly: int button1State, button2State; //variables to hold the button states button1State = digitalRead(button1Pin); //read the inputs button2State = digitalRead(button2Pin); if(button1State == LOW){ //if button1 is pressed turn on LED1 digitalWrite(led1Pin, HIGH); } else{ //otherwise turn off the LED digitalWrite(led1Pin, LOW); } if(button2State == LOW){ //if button2 is pressed turn on LED2 digitalWrite(led2Pin, HIGH); } else{ //otherwise turn off the LED digitalWrite(led2Pin, LOW); }}
29
roboticsgateway.org
Servos
• A servo is a type of electric motor that allows for precise angular motion– Servos will hold their given
position unlike motors which may continue to rotate
• Red=power• Black=ground• Yellow=control
30
roboticsgateway.org
Schematic
31
roboticsgateway.org
Moving the servo
• Commands– #include <Servo.h>
• inserts the Servo library into the code– Servo servo1;
• create a “new” servo in the code– servo1.attach(servoPin);
• set the digital pin the servo is attached to
– servo1.write(pos);• servos can move to a specific position
between 0 and 180 degrees• make sure to give the servo a short delay
so it has time to make the move
32
roboticsgateway.org
Basic Servo Code
#include <Servo.h>
Servo myservo;
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
myservo.write(180);
delay(1000);
myservo.write(0);
delay(1000);
}
33
roboticsgateway.org
Challenge: Servo+Potentiometer
• Create a program that makes the servo turn when you turn the potentiometer.
• map(var, currentLow, currentHigh, newLow, newHigh);
34
roboticsgateway.org
Servo+Potentiometer Solution Code
#include <Servo.h>
Servo myservo;
int sensorPin=0;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int sensorValue = analogRead(sensorPin); //recieve the potentiometer value
sensorValue = map(sensorValue, 0, 1023, 0, 180); //scale the potentiometer reading to a possible servo value
myServo.write(sensorValue); // write the position to the servo
delay(15);
}
35
roboticsgateway.org
Intro to Arduino
Thanks for Coming!Visit our website if you are interested in signing up for more classes!
Web Resources:
• https://www.arduino.cc/
36