GP106 - Arduino programming with matlab pdn ac lk
GP106 - Arduino programming with matlab pdn ac lk
Size: 1.28 MB
Language: en
Added: Oct 10, 2024
Slides: 21 pages
Slide Content
Microcontroller Programming
with Arduino and Matlab
Microcontrollers
•A microcontroller is a small-scale
computer with generalized (and
programmable) inputs and outputs.
•The inputs and outputs can be
manipulated by and can manipulate the
physical world.
T
h
e
B
o
x
M
e
e
t
A
r
d
u
in
o
U
n
o
Analog
INPUTS
Digital I/O
PWM (3, 5, 6, 9, 10, 11)
PWR IN USB
(to Computer)
SCL/SDA
(I2C Bus)
POWER
5V / 3.3V / GND
RESET
IO Pins
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Input/Output
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Breadboard
•Each row (horiz.) of
5 holes are
connected
•Vertical columns –
called power bus are
connected vertically
Programming the Microcontroller
Getting Started
1.Make your circuit with the Arduino and the prototyping
(bread) board
2.Connect the board to your computer via the USB cable
3.Find the serial port
4.Write you Matlab program
–Connect Arduino [a = arduino('COM3')]
–Set Pin Mode [pinMode(a,pin,str)]
–Read/Write Digital/Analog Data
•dVal = digitalRead(a,pin)
•digitalWrite(a,pin,val)
•aVal = analogRead(a,pin)
•analogWrite(a,pin,val)
5.Run
Testing the Board
•You may test the board by turning on and off
the ‘test’ LED that is connected to digital pin
#13 .
•Matlab code:
a = arduino('COM3');
pinMode(a,13,'OUTPUT');
digitalWrite(a,13,0);
pause(1);
digitalWrite(a,13,1);
Ex1: Connecting an External LED
You can try the same code from Testing the Board program
220 Ohm Resistor (Red-Red-Brown)
LED – long-leg (anode, +), short-leg (cathode, -)
Ex2: Connecting Two External LEDs
•Now, let’s connect another LED to pin
#11 with a serial register as earlier.
•The Matlab code
a = arduino('COM3');
pinMode(a,13,'OUTPUT');
pinMode(a,11,'OUTPUT');
digitalWrite(a,11,0); digitalWrite(a,13,0);
pause(1);
digitalWrite(a,11,1); digitalWrite(a,13,1);
•Can a digital device produce analog output?
Analog Output
•Analog output can be simulated using
pulse width modulation (PWM)
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Pulse Width Modulation
•Can’t use digital pins
to directly supply say
2.5V, but can pulse
the output on and off
really fast to produce
the same effect
•The on-off pulsing
happens so quickly,
the connected output
device “sees” the
result as a reduction
in the voltage
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
PMW Pins
•Command:
analogWrite(a,pin,va
l)
•value is duty cycle:
between 0 and 255
•Examples:
analogWrite(a,9,128)
for a 50% duty cycle
analogWrite(a,9,64)
for a 25% duty cycle
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Ex3: Analog Output to LED
•We will use the same circuit build for
example 2. However, we will use
analogWrite()
•Matlab Code:
a = arduino('COM3');
pinMode(a,11,'OUTPUT');
analogWrite(a,11,0);
analogWrite(a,11,128);
analogWrite(a,11,255);
Sensors
•Typically sensors are analog
–Temperature measurement
–Light intensity measurement
•Arduino uses a 10-bit A/D Converter:
–this means that you get input values from 0
to 1023
•0 V 0
•5 V 1023
•A two pin analog sensor will be a variable
resistor
Two Pin Analog Sensors
LDR
A0
Ex4: analogRead() from LDR
•Connect the circuit as in the last slide
•Matlab Code:
a = arduino('COM3');
analogRead(a,0);