Professional Documents
Culture Documents
ArduinoIOv25 PDF
ArduinoIOv25 PDF
giampiero.campa@mathworks.com
2
Sensing & data acquisition
3
Processing & programming platform
Computer
– Connection to DAQ (USB, PCI, Serial, Wireless)
– Programming-Analysis Environment
– OS / Drivers (Real Time ?)
– Embedded / Small Form Factor Computer
4
Acting (digital & analog outputs, PWM)
Relays
Motors
– DC/Steppers/Servos
– Signal Amplification
– Power Suppliers, Batteries
– Forward/Reverse, H-Bridges
Displays
5
Lots of (interrelated) choices
Driving Factors:
Cost
Easy of Use
Flexibility
6
Outline
7
Arduino
8
Specifications (Arduino Uno)
ATmega328 microcontroller
16 MHz, 32 KB FLASH, 2KB SRAM, 1K EEPROM
19 DIO pins (6 can be 8-bits 500Hz PWM outputs)
6 analog inputs (10 bits over 0-5V range, 15kSPS)
5V operating voltage, 40 mA DC Current per IO Pin
I2C (TWI) fully supported and SPI partially supported
Atmega16U2 acting as a USB-to-serial converter
The OS sees it as a virtual serial port
Power jack and optional 9V power supplier
9
Shields
Examples:
– Motor/Stepper/Servo Shields (Motor Control)
– Multichannel Analog and Digital IO Shields
– Prototyping Shields
– Ethernet and Wireless communication Shields
– Wave Shields (Audio)
– GPS Logging and Accelerometer Shields
– Relay Control Shields
10
What is Arduino good for ?
11
What is Arduino good for ?
12
Outline
13
Same board … different approaches
14
MathWorks packages
Arduino IO Package
– Requires: MATLAB, Optional: Simulink
– Approach: Tethered mode, Main Use: IO from MATLAB.
This is the package explained in this presentation.
Arduino answers to
MATLAB via USB
16
The Arduino server sketch
Request
Answer
17
Which sketch do I need to upload ?
18
Using MATLAB vs. the Arduino IDE
19
Using MATLAB vs. the Arduino IDE
20
Outline
21
Connect
Examples:
>> a=arduino('COM5');
>> a=arduino('/dev/ttyS101');
Examples:
>> pinMode(a,11,'output')
>> pinMode(a,10,'input')
>> a.pinMode(10,'input')
>> val=pinMode(a,10)
>> pinMode(a,5)
>> pinMode(a);
23
Digital read (digital input)
Examples:
>> val=digitalRead(a,4)
>> val=a.digitalRead(4)
24
Digital read example
1
25
Digital write (digital output)
Examples:
digitalWrite(a,13,1); % sets pin #13 high
a.digitalWrite(13,1); % sets pin #13 high
digitalWrite(a,13,0); % sets pin #13 low
26
Digital write example
MATLAB
Command
Window
>> digitalWrite(a,9,1)
>> digitalWrite(a,9,0)
Led Off Led On
27
Analog read (analog input)
Examples:
val=analogRead(a,0); % reads analog pin #0
val=a.analogRead(5); % reads analog pin #5
28
Analog read example
MATLAB
>> analogRead(a,3)
Command
Window ans =
285
>> analogRead(a,3)
ans =
855
29
Analog (PWM) write (analog output)
Examples:
analogWrite(a,11,90); % sets pin #11 to 90
a.analogWrite(11,90); % sets pin #11 to 90
analogWrite(a,3,10); % sets pin #3 to 10
MATLAB
Command
Window
>> analogWrite(a,9,10)
>> analogWrite(a,9,50)
31
Disconnect
Examples:
>> delete(a);
>> a.delete;
32
Example : the blink challenge project
33
Schematic
34
Information flow
MATLAB
OUTPUTS
INPUTS
USER
35
MATLAB code
% initialize pins % get previous state
disp('Initializing Pins ...'); prev=a.digitalRead(bPin);
% sets digital and analog (pwm) output pins % read analog input
a.pinMode(5, 'OUTPUT'); % pwm available here ain=a.analogRead(aPin);
a.pinMode(6, 'OUTPUT'); % pwm available here v=100*ain/1024;
a.pinMode(9, 'OUTPUT'); % pwm available here
a.pinMode(10,'OUTPUT'); % pwm available here % read current button value
a.pinMode(11,'OUTPUT'); % pwm available here % note that button has to be kept pressed a few seconds to make sure
a.pinMode(12,'OUTPUT'); % the program reaches this point and changes the current button value
a.pinMode(13,'OUTPUT'); curr=a.digitalRead(bPin);
% button pin and analog pin % button is being released, change state
bPin=2;aPin=2; % delay corresponds to the "on" time of each led in state 3 (wave)
if (curr==1 && prev==0),
% initialize state state=mod(state+1,4);
state=0; disp(['state = ' num2str(state) ', delay = ' num2str(v/200)]);
end
36
MATLAB code
% wave
% toggle state all on or off
if (state==3),
if (state<2), for i=4:8,
for i=9:13, a.digitalWrite(9+mod(i,5),0);
a.digitalWrite(i,state); a.digitalWrite(9+mod(i+1,5),1);
end pause(v/200);
end
end
a.digitalWrite(13,0);
end
37
Prototyping shield
Button to
Potentiometer change
to regulate mode
on/off LEDs
delay
Arduino
Board
underneath
39
Outline
40
Motors and motor shields, quick facts
Arduino
External Jumper on == same Board
Power power source for both underneath
Connector Arduino and motor shield
42
Example: Adafruit motor shield v1 specs
Examples:
motorSpeed(a,4,200);
val=motorSpeed(a,1);
motorSpeed(a,3);
a.motorSpeed;
Examples:
motorRun(a,1,'forward');
motorRun(a,3,'backward');
a.motorRun(3,'backward');
motorRun(a,1,'release');
46
DC motor (Adafruit motor shield): example
(running)
Motor #3
47
Stepper motor speed (Adafruit motor shield)
Examples:
stepperSpeed(a,2,50)
val=stepperSpeed(a,1);
a.stepperSpeed(3);
stepperSpeed(a);
Examples:
stepperStep(a,1,'forward','interleave',50);
a.stepperStep(1,'forward','microstep',200);
stepperStep(a,1,'backward','double',200);
stepperStep(a,1,'release');
49
Stepper motor (Adafruit motor shield):
example
MATLAB >> stepperSpeed(a,1,50)
Command
Window: >> stepperStep(a,1,'backward','microstep',100)
Stepper #1 Coil #1
Coil #2
50
Servo motors, quick facts
51
Servo motors, more quick facts
52
Servo status (attached/detached)
Examples:
val=servoStatus(a,9);
servoStatus(a,10);
a.servoStatus(11);
servoStatus(a);
53
Servo attach
Examples:
servoAttach(a,10); % attach servo to pin 10
a.servoAttach(10); % attach servo to pin 10
servoAttach(a,9); % attach servo to pin 9
54
Servo read
Examples:
val=servoRead(a,10); % read angle servo #10
val=a.servoRead(10); % read angle servo #10
val=servoRead(a,9); % read angle servo #9
55
Servo write
Examples:
servoWrite(a,10,45); % rotate servo 10 to 45°
a.servoWrite(10,70); % rotate servo 10 to 70°
56
Servo detach
Examples:
servoDetach(a,10); % detach servo #10
a.servoDetach(10); % detach servo #10
servoDetach(a,9); % detach servo #9
57
Servo example (with Adafruit motor shield v1)
>> servoWrite(a,9,180)
>> servoRead(a,9)
ans =
Servo #2 180
(attached to pin 9)
58
Encoder quick facts
59
Encoder attach
Example:
60
Encoder status (attached/detached)
Examples:
encoderDetach(a,0); % detach encoder #0
a.encoderDetach(1); % detach encoder #1
61
Encoder read
Examples:
val=encoderRead(a,2); % reads encoder #2
val=a.encoderRead(0); % reads encoder #0
Examples:
encoderReset(a,1); % resets encoder #1
a.encoderReset(2); % resets encoder #2
63
Encoder detach
Examples:
encoderDetach(a,0); % detach encoder #0
a.encoderDetach(1); % detach encoder #1
64
Encoder example
>> encoderAttach(a,0,3,2)
>> encoderRead(a,0)
ans =
MATLAB
Command 0
Window:
>> encoderRead(a,0)
Encoder terminals A and B attached to
pins 3 and 2 ans =
44
Encoder
terminal C
(common)
attached to
ground
65
Outline
66
Arduino IO Simulink library
12 blocks:
Setup and Pacer
Analog and Digital IO
Servos
Encoders
AF Motor Shield
68
Example: blink challenge using Stateflow
69
Outline
70
Server sketch: basic architecture
void loop() {
val = one-byte input
… from the serial port
if (Serial.available() >0) {
val = Serial.read();
s = state
(initialized to -1)
switch (s) {
…
case 20: Calculate next state and
action (20,val) perform an action based
on both current state and
s (20,val) input
…
71
Using the server with a serial monitor
Request
Answer
72
Using the server with a serial monitor
Examples:
– 1c : reads digital pin #2 (c)
– 2n1 : sets digital pin #13 (n) high
– 4jz : analog output on pin #9 (j) to 122=ascii(z) over 255
– 6j1 : attaches servo on pin #9
– A1z : sets speed of motor #1 to 122 over 255 (122=ascii(z))
– D1fsz : does 122 steps on motor #1 forward in single (s) mode
– E0cd : attaches encoder #0 (0) on pins 2 (c) and 3 (d)
– G0 : gets position of encoder #0
– R2 : sets analog reference to EXTERNAL
73
Customizing Arduino IO: roundTrip
Receives byte
ans =
Arduino boards:
– Duemilanove
– Uno
– Mega 2560
– Nano
– Due (only adio.pde and adioe.pde)
– Other boards like Mini or Pro may work but have not been tested.
Digilent ChipKit boards:
– Uno32 (adio.pde only)
– Max32 (adio.pde only)
– Cerebot (adio.pde only)
See the readme.txt file for more info
75
Useful links: buying an Arduino
Sparkfun: https://www.sparkfun.com/
Robotshop: http://www.robotshop.com/
76
Useful links: buying motors
77
Useful links: getting started guides
78
Conclusions
79