79
Arduino An Introduction to Engineers Hong Zhang, PhD Mechanical Engineering Rowan University

Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Arduino An Introduction to Engineers

Hong Zhang, PhD

Mechanical Engineering

Rowan University

Page 2: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

What is Arduino

• Arduino is an open-source electronics prototyping

platform based on flexible, easy-to-use hardware

and software.

Page 3: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Peripherals

USB Cord

Battery Wires

Breadboard

Wire Stripper

Page 4: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Amazon Kit

Page 5: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Sparkfun Kit

Page 6: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Soldering

Solder

Stand

Perfboard Long nose plier

Solder Remover Iron

Page 7: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Variations

LilyPad

Mini Esplora

Due

Page 8: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Shields

Touchpad Joystick

Cellphone LCD TinkerKit

Page 9: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 10: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Clones

Motoruino

Maple Boarduino

Roboduino Freeduino

Sanguino

Page 11: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

USB Jack

Transmission status LED

Pin 13 LED

Power Jack

Reset

Microcontroller ATMEGA328

Power Pins Analog Input Pins

Digital I/O Pins

ICSP Header

Power LED

Voltage Regulator

USB Chip

Crystal

Page 12: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 13: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Verify the grammar

Upload to the Arduino board

New file

Open a file

Save the current file

Open up the serial monitor window

Code

Message Serial port

Page 14: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Use Arduino

Generate Idea

Make Plans

Write Code

Upload & Test

Design Circuit

Review, rebuild, modification

Project Goal

Page 15: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Arduino References

• www.arduino.cc

Page 17: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

• Don’t put Arduino board on metal surface!

• Unplug power first before making any change on

circuit.

• If you smell something, see smoke or flame, touch

something burning, then something must be wrong!

Disconnect the power first.

1 Safety

Page 19: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Learn From Examples • Online:

http://arduino.cc/en/Tutorial/HomePage

• Or from local Examples

Page 20: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Blink Example /*

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

}

Page 21: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

pinMode(pin#, OUTPUT) • The specified pin can provide a substantial amount

of current to other circuits.

• Current is up to 40 mA (milliamps) to other

devices/circuits.

• Enough for an LED (don't forget the series resistor)

• Not enough to run most relays, solenoids, or motors.

Page 22: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

pinMode(pin#, INPUT) • Default if not specified

• Avoid pin 13 since it’s assigned to an onboard LED.

Pin 13 LED

Page 23: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

digitalWrite()

• digitalWrite(Pin#, HIGH); o Set the pin to high voltage (either 3.3V or 5V)

• digitalWrite(Pin#, LOW); o Set the pin to low voltage (0V)

Page 24: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

LED

http://www.societyofrobots.com/electronics_led_tutorial.shtml

Page 25: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

External LED

Page 26: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Read Resistor

Page 27: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 28: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Blink 1 LED

1. Put 1 LED on the breadboard

2. Connect the LED to pin 10 of the Arduino

3. Blink the LEDs 10 & 13 together

4. Change the duration and intervals of blinking

5. Generate Morse code “· · · — — — · · ·”

Page 29: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Using Constants const int ontime = 2000; // LED ON time

const int offtime = 2000; // LED OFF time

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(ontime); // wait

digitalWrite(13, LOW); // set the LED off

delay(offtime); // wait

}

Page 30: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Using Variables int ontime, offtime; // LED ON and OFF time

void setup() {

pinMode(13, OUTPUT);

ontime = 1000;

offtime = 1000;

}

void loop() {

digitalWrite(13, HIGH); // set the LED on

delay(ontime); // wait

digitalWrite(13, LOW); // set the LED off

delay(offtime); // wait

}

Page 31: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

220Ω 220Ω

220Ω

+ +

+ _

_

_

Page 32: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Blink 3 LEDs 1. Put 1 Red LED, 1 Green LED, and 1 yellow LED on

the breadboard

2. Connect the LEDs to pins 8, 7, 6

3. Blink the LEDs together

4. Change the duration and intervals of blinking

Page 33: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Flow Control

for (int i=6; i<9; i++) {

for (int j=6; j<9; j++) {

digitalWrite(j, LOW);

}

digitalWrite(i, HIGH);

delay(flashtime);

}

Page 34: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Blink the LEDs Sequentially

1. Follow the order RGYRGYRGY…

2. Combine order with duration and interval.

3. Synchronize with a song of choice.

Page 35: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Read Digital Pin High

Page 36: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

const int buttonPin = 6; // the number of the input pin

const int ledPin = 10; // the number of the LED pin

int buttonState = 0; // variable for reading the input status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop(){

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

digitalWrite(ledPin, buttonState);

}

Page 37: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Operation

• 1 – 0 = 1;

• 1 – 1 = 0;

• !1 = 0;

• !0 = 1;

• 1 – LOW = HIGH;

• 1 – HIGH = LOW;

• !HIGH = LOW;

• !LOW = HIGH;

HIGH = 1

LOW = 0

Page 38: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Read Digital Pin Low

Page 39: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

buttonState = !buttonState;

Or

buttonState = 1-buttonState;

Page 40: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Switch

Page 41: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Push To Turn ON

Page 42: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Push To Turn OFF

Page 43: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

if … else…

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

Page 44: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Comparison Operations

>

<

==

!=

>=

<=

Page 45: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Optical Switch

Page 46: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Infrared Light of Optical Sensor

Page 47: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Optical Sensor

82Ω 47kΩ

A

K E

C

GND

+5V

Output White wire

Page 48: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

82Ω 47kΩ

Page 49: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

ON With Reflection

Page 50: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

OFF With Reflection

Page 51: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Encoder

T (s)

Voltage

5V or HIGH

Page 52: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Non-Reflective

T (s)

Voltage

0V or LOW

Page 53: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Moving Reflections

T (s)

Voltage

0V or LOW

5V or HIGH

Page 54: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

T (s)

Voltage

5V or HIGH

Page 55: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

T (s)

Voltage

0V or LOW

Page 56: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

T (s)

Voltage

0V or LOW

5V or HIGH

ω

Page 57: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

T (s)

Voltage

0V or LOW

5V or HIGH

ω

Φ

Δt Δt

Page 58: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Angular Velocity

 

RPM =w *60/2p

Page 59: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Φ

• Determined from the daisy wheel design

Φ

Φ=2π/8

Page 60: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Other Encoder Wheels

High resolution

Multi-digit

Page 61: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Got Time?

unsigned long ms, us;

ms = millis(); //obtain the time in milli second since the

program started

us = micros(); // obtain the time in micro second

since the program started

Page 62: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Δt

• In the Setup()

time_last = 0; // Set the base time

• In the Loop()

time_current = millis(); //Get the current time

// After a trigger such as a rising or falling edge

Delta_t = time_current - time_last;

time_last = time_current; //New base time

Page 63: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Obtain a Trigger

• In Setup()

Optical_last = HIGH; //initial base status

• In Loop()

Optical_current = digitalRead(OpticalPin); // get current status of the pin

If (Optical_current != Optical_last) { //Is it a change?

Optical_last = Optical_current; //new base status

}

Page 64: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Serial Monitor

• Display the values of variables on a monitor

• Serial.begin(9600); // Start the serial monitor to get

feedback at 9600bit per second.

Page 65: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Set the serial monitor at the same baud rate

Page 66: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Speed Compares (bit/s)

• Serial: 2400, 9600, 19200, etc

• USB:1.5M, 12M, 3G, 10G

• Wi-Fi: 7.2M, 72.2M, 150M, 866.7M,

• Eithernet: 10M, 1G, 100G

• Harddrive (SATA): 1.5G, 3G, 6G

• Memory: 102G

• Note 1mB (MegaByte)=8mb

Page 67: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Display Values

Serial.print(”text"); // Display text in verbatian

Serial.print(variable); // Display the value of a variable

Serial.println(”text"); // Display text in verbatian then

change line

Serial.println(variable); // Display the value of a

variable then change line

Page 68: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Display Time In Serial Monitor

unsigned long ms, us;

int interval;

void setup() {

Serial.begin(9600); // Start the serial monitor to get feedback

// Make sure the baud rate of the serial monitor is set as 9600.

interval = 2000; // Initialize the interval between display

}

void loop() {

ms = millis(); //obtain the time in milli second since the program started

us = micros(); // obtain the time in micro second since the program started

Serial.print("Time: ms= "); // For easy reading on serial monitor

Serial.print(ms); // Display the time on the serial monitor

Serial.print(" , us= "); // Note that print() will just display variables

Serial.println(us); // println() will change line

delay(interval); // wait for next display

}

Page 69: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Encoder const int OpticalPin = 7; // the number of the pushbutton pin

const int ledPin = 13; // the number of the LED pin

unsigned long time_last, time_current; // variable to record time

int Optical_last, Optical_current; // variable for reading the optical sensor status

float phi = PI/8; // measured angle of each windlow on the wheel

float omega, rpm;

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(OpticalPin, INPUT);

Serial.begin(9600); // Start the serial monitor to get feedback

time_last = 0; // initialize the time

Optical_last = HIGH;

}

void loop(){

// read the state of the Optical sensor status:

Optical_current = digitalRead(OpticalPin);

digitalWrite(ledPin, Optical_current); //LED indicator of the speed

if (Optical_current != Optical_last) { // for faster operation, we can use Optical_current & Optical_last

time_current = millis(); //read the current time

omega = 1000* phi / (time_current - time_last); // calculate the angular velocity

rpm = omega * 30 / PI; // convert angular velocity to RPM

time_last = time_current;

Optical_last = Optical_current;

Serial.print("omega= "); // Display the result to serial monitor.

Serial.print(omega);

Serial.print("rad/sec, or ");

Serial.println(rpm);

}

}

Page 70: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Analog Read Int potPin = A0; // select the input pin for the

potentiometer. Total 6 pins from A0 to A5

Analog Input Pins

Page 71: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Analog Read potValue = analogRead(potPin); // read the value

from the potPin and store it in the variable

potValue.

An integer range from 0 (0V) to 1023 (5V). The resolution is 4.9mV per unit.

Page 72: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 73: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Use a Pot to Control an LED int potPin = A0; // select the input pin for the potentiometer

int ledPin = 13; // select the pin for the LED

int potValue = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT

Serial.begin(9600); // Start the serial monitor to get feedback

}

void loop() {

potValue = analogRead(potPin); // read the value from the potentiometer

Serial.print("Potentiometer value is ");

Serial.println(potValue);

digitalWrite(ledPin, HIGH); // turn the ledPin on

delay(potValue); // stop the program for <potValue> milliseconds

digitalWrite(ledPin, LOW); // turn the ledPin off

delay(potValue); // stop the program for for <potValue> milliseconds

}

Page 74: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 75: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Servo

#include <Servo.h> // First need to include a servo

header file.

Servo myservo; // Create a servo object myservo

myservo.attach(7); // attaches the servo on pin 7 to

the servo object

Page 76: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

myservo.write(val);

// Set the servo position according to the variable val.

The range of val is from 0 to 179.

To ensure the range of val, we can map it from any

other values, e.g., the reading from a pot:

val = map(val, 0, 1023, 0, 179);

Page 77: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message
Page 78: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message

Use a Pot to Control a Servo

#include <Servo.h> // Include the servo header file

Servo myservo; // create servo object to control a servo

int potpin = A0; // analog pin used to connect the potentiometer

int val; // variable to read the value from the analog pin

void setup()

{

myservo.attach(7); // attaches the servo on pin 7 to the servo object

}

void loop()

{

val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)

val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)

myservo.write(val); // sets the servo position according to the scaled value

delay(15); // waits for the servo to get there

}

Page 79: Arduino - PBworksmymechatronics.pbworks.com/w/file/fetch/73929446/Arduino.pdfArduino board New file Open a file Save the current file Open up the serial monitor window Code Message