arduino - pbworksmymechatronics.pbworks.com/w/file/fetch/73929446/arduino.pdfarduino board new file...

Post on 04-Apr-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Arduino An Introduction to Engineers

Hong Zhang, PhD

Mechanical Engineering

Rowan University

What is Arduino

• Arduino is an open-source electronics prototyping

platform based on flexible, easy-to-use hardware

and software.

Peripherals

USB Cord

Battery Wires

Breadboard

Wire Stripper

Amazon Kit

Sparkfun Kit

Soldering

Solder

Stand

Perfboard Long nose plier

Solder Remover Iron

Variations

LilyPad

Mini Esplora

Due

Shields

Touchpad Joystick

Cellphone LCD TinkerKit

Clones

Motoruino

Maple Boarduino

Roboduino Freeduino

Sanguino

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

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

Use Arduino

Generate Idea

Make Plans

Write Code

Upload & Test

Design Circuit

Review, rebuild, modification

Project Goal

Arduino References

• www.arduino.cc

• 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

Learn From Examples • Online:

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

• Or from local Examples

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

}

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.

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

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

Pin 13 LED

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)

LED

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

External LED

Read Resistor

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 “· · · — — — · · ·”

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

}

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

}

220Ω 220Ω

220Ω

+ +

+ _

_

_

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

Flow Control

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

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

digitalWrite(j, LOW);

}

digitalWrite(i, HIGH);

delay(flashtime);

}

Blink the LEDs Sequentially

1. Follow the order RGYRGYRGY…

2. Combine order with duration and interval.

3. Synchronize with a song of choice.

Read Digital Pin High

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);

}

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

Read Digital Pin Low

buttonState = !buttonState;

Or

buttonState = 1-buttonState;

Switch

Push To Turn ON

Push To Turn OFF

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);

}

Comparison Operations

>

<

==

!=

>=

<=

Optical Switch

Infrared Light of Optical Sensor

Optical Sensor

82Ω 47kΩ

A

K E

C

GND

+5V

Output White wire

82Ω 47kΩ

ON With Reflection

OFF With Reflection

Encoder

T (s)

Voltage

5V or HIGH

Non-Reflective

T (s)

Voltage

0V or LOW

Moving Reflections

T (s)

Voltage

0V or LOW

5V or HIGH

T (s)

Voltage

5V or HIGH

T (s)

Voltage

0V or LOW

T (s)

Voltage

0V or LOW

5V or HIGH

ω

T (s)

Voltage

0V or LOW

5V or HIGH

ω

Φ

Δt Δt

Angular Velocity

 

RPM =w *60/2p

Φ

• Determined from the daisy wheel design

Φ

Φ=2π/8

Other Encoder Wheels

High resolution

Multi-digit

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

Δ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

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

}

Serial Monitor

• Display the values of variables on a monitor

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

feedback at 9600bit per second.

Set the serial monitor at the same baud rate

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

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

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

}

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);

}

}

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

potentiometer. Total 6 pins from A0 to A5

Analog Input Pins

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.

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

}

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

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);

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

}

top related