SlideShare a Scribd company logo
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.
The
Box
Meet
Arduino
Uno
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);
Ex5: LDR (analog) -> LED (analog)
THANK YOU!

More Related Content

PPT
arduino.ppt
PDF
introductiontoarduino-111120102058-phpapp02.pdf
KEY
Intro to Arduino
PPT
Fundamentals of programming Arduino-Wk2.ppt
PPT
Arduino_CSE ece ppt for working and principal of arduino.ppt
PPT
Arduino wk2
PPTX
Introduction to the Arduino
PPTX
Introduction To Arduino-converted for s.pptx
arduino.ppt
introductiontoarduino-111120102058-phpapp02.pdf
Intro to Arduino
Fundamentals of programming Arduino-Wk2.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino wk2
Introduction to the Arduino
Introduction To Arduino-converted for s.pptx

Similar to GP106 - Arduino programming with matlab pdn ac lk (20)

PPT
Arduino is an open-source electronics platform that has an easy-to-use physic...
PPTX
arduinoedit.pptx
PDF
Intro to Arduino Revision #2
PPTX
PPTX
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
PPTX
INTRODUCTION TO ARDUINO and sensors for arduino.pptx
PPT
01 Intro to the Arduino and it's basics.ppt
PDF
Starting with Arduino
PPT
ArduinoSectionI-slides.ppt
PPTX
Audible Objects
PPTX
Arduino board program for Mobile robotss
PPTX
Introduction to arduino
PDF
Arduino-workshop.computer engineering.pdf
PDF
02 Sensors and Actuators Understand .pdf
PDF
Introduction of Arduino Uno
PPTX
Arduino Programming Familiarization
PDF
Arduino Comic-Jody Culkin-2011
PDF
Arduino comic v0004
PPTX
Introduction to Arduino Microcontroller
PPT
computer_programming for grade 12 learners
Arduino is an open-source electronics platform that has an easy-to-use physic...
arduinoedit.pptx
Intro to Arduino Revision #2
INTODUCTION OF IOT AND INTERFACING WITH ARDUINO UNO
INTRODUCTION TO ARDUINO and sensors for arduino.pptx
01 Intro to the Arduino and it's basics.ppt
Starting with Arduino
ArduinoSectionI-slides.ppt
Audible Objects
Arduino board program for Mobile robotss
Introduction to arduino
Arduino-workshop.computer engineering.pdf
02 Sensors and Actuators Understand .pdf
Introduction of Arduino Uno
Arduino Programming Familiarization
Arduino Comic-Jody Culkin-2011
Arduino comic v0004
Introduction to Arduino Microcontroller
computer_programming for grade 12 learners
Ad

Recently uploaded (20)

PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Empowerment Technology for Senior High School Guide
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
IGGE1 Understanding the Self1234567891011
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Lesson notes of climatology university.
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
advance database management system book.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Chinmaya Tiranga quiz Grand Finale.pdf
Empowerment Technology for Senior High School Guide
202450812 BayCHI UCSC-SV 20250812 v17.pptx
IGGE1 Understanding the Self1234567891011
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Supply Chain Operations Speaking Notes -ICLT Program
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Lesson notes of climatology university.
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
advance database management system book.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Final Presentation General Medicine 03-08-2024.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Ad

GP106 - Arduino programming with matlab pdn ac lk

  • 2. 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.
  • 4. Meet Arduino Uno 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
  • 5. IO Pins Image from Theory and Practice of Tangible User Interfaces at UC Berkley
  • 6. Input/Output Image from Theory and Practice of Tangible User Interfaces at UC Berkley
  • 7. Breadboard • Each row (horiz.) of 5 holes are connected • Vertical columns – called power bus are connected vertically
  • 9. 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
  • 10. 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);
  • 11. 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, -)
  • 12. 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);
  • 13. • 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
  • 14. 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
  • 15. 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
  • 16. 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);
  • 17. 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
  • 18. Two Pin Analog Sensors LDR A0
  • 19. Ex4: analogRead() from LDR • Connect the circuit as in the last slide • Matlab Code: a = arduino('COM3'); analogRead(a,0);
  • 20. Ex5: LDR (analog) -> LED (analog)