Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is...

36
roboticsgateway.org @roboticsgateway Facebook.com/roboticsgateway Introduction to Arduino

Transcript of Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is...

Page 1: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org @roboticsgatewayFacebook.com/roboticsgateway

Introduction to Arduino

Page 2: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Setup

– Arduino IDE Downloadhttps://www.arduino.cc/en/Main/Software

2

Page 3: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 4: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Circuits

4

Page 5: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Terminology

5

Page 6: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 7: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 8: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Breadboards

8

Page 9: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

• Make a basic circuit with a blinking LED

• Learn how to program pins on the Arduino

Example 1: Blinking an LED

9

Page 10: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Schematic

10

Page 11: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

pinMode(13, OUTPUT);

• Sets pin 13 as an output pin• Put this in the setup

11

Page 12: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

digitalWrite(13, HIGH);

• Sets pin 13 to provide high current (turns on LED)

• Put this in your loop

12

Page 13: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

delay(1000);

• Delays the program (pauses it) for 1000 milliseconds (1 second)

• Put this in your loop

13

Page 14: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

digitalWrite(13, LOW);

• Sets pin 13 to provide no current (turns off LED)

• Put this in your loop

14

Page 15: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

void setup() {pinMode(13, OUTPUT);

}

void loop() {digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000);

}

15

Page 16: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 17: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 18: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 19: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Schematic

19

Page 20: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 21: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Blinking LED w/ Potentiometer Code

21

Page 22: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 23: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

If/Else Statements

if(condition){//do something

}else if(condition 2){

//do something else}else{

//so some third thing}

23

Page 24: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

If/Else Statements

Comparators: ==, !=, &&, ||, !

24

Page 25: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 26: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Schematic

26

Page 27: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

On/Off Buttons Code

27

Page 28: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 29: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 30: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 31: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

roboticsgateway.org

Schematic

31

Page 32: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 33: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 34: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 35: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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

Page 36: Introduction to Arduino - Robotics Gateway · roboticsgateway.org What is Arduino? • Arduino is an open-source prototyping platform based on easy to use hardware and software •

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