Arduino and Matlab for prototyping and -...
Transcript of Arduino and Matlab for prototyping and -...
Arduino and Matlab for prototyping and manufacturing
Enrique Chacón Tanarro
11th - 15th December 2017 UBORA First Design School - Nairobi Enrique Chacón Tanarro
Index
1. Arduino
2. Arduino programming
3. Matlab controlling arduino
11th - 15th December 2017 UBORA First Design School - Nairobi 2
1. Ardunio
Arduino is an open-source electronic platform based on easy to use hardware and software (https://www.arduino.cc/ )
11th - 15th December 2017 UBORA First Design School - Nairobi 3
ENVIROMENT
Sensors:
• Temperature • Pressure • Light • Proximity • Acceleration • …
Actuators:
• Motors • Relays • Valves • Piezoelectrics • Diode led • …
ARDUINO ENVIROMENT INTERACTION
1.1. Enviroment
Arduino is an open-source electronic platform based on easy to use hardware and software (https://www.arduino.cc/ )
11th - 15th December 2017 UBORA First Design School - Nairobi 4
ENVIROMENT
Sensors:
• Temperature • Pressure • Light • Proximity • Acceleration • …
Actuators:
• Motors • Relays • Valves • Piezoelectrics • Diode led • …
ARDUINO ENVIROMENT INTERACTION
1.1. Enviroment
Arduino can receive environment information from 2 ways:
A) USER
• Physical: switches, buttons, potentiometers,…
• Virtual
11th - 15th December 2017 UBORA First Design School - Nairobi 5
Arduino PC - Matlab
1.1. Enviroment
Arduino can receive environment information from 2 ways:
B) SENSORS
11th - 15th December 2017 UBORA First Design School - Nairobi 6
Temperature
Termoresistance
Light
Fotoresistence
Pressure
Piezoelectric
Fluids Mechanical contact
Position /
Velocity
Encoder
Aceleration
Accelerometers
Movement
Movement
sensor Sound
Microphone
Gas
1.1. Enviroment
EXAMPLE: How can we measure the luminosity of this room?
11th - 15th December 2017 UBORA First Design School - Nairobi 7
LDR resistance varies its resistance as a function of light
A0 pin voltage depends on the LDR resistance
Light Ω
1.2. Arduino board
What does ARDUNIO do with the information from sensors, touchscreens, etc.
11th - 15th December 2017 UBORA First Design School - Nairobi 8
ENVIROMENT
Sensors:
• Temperature • Pressure • Light • Proximity • Acceleration • …
Actuators:
• Motors • Relays • Valves • Piezoelectrics • Diode led • …
ARDUINO ENVIROMENT INTERACTION
1.2. Arduino board
11th - 15th December 2017 UBORA First Design School - Nairobi 9
https://www.robomart.com
Arduino ONE board
Digital Information
Programmable integrated circuit
Fundamental parts: • CPU (calculation speed) • Memory (code complexity) • Input and output peripherals
(number of signals to be controlled
Analogical Information
Arduino board selection
1.2. Arduino board
11th - 15th December 2017 UBORA First Design School - Nairobi 10
Arduino board selection
• Arduino Uno
• Arduino ProMini
• Arduino Nano
• Arduino Micro
• Arduino Mega 2560
• Arduino Mega ADK (android)
• Arduino Gemma
• Arduino Leonardo
• Arduino Yún (ethernet)
• Arduino Cactus (WiFi)
1.2. Arduino board
11th - 15th December 2017 UBORA First Design School - Nairobi 11
Arduino UNO board
Analogical input Digital input/output
• 6 input pins A0,A1,A2,...
• Voltage range: 0 - 5V
• Precision: 5V / 1024 values = 5mV
• 14 pins 0-13
• Sensors and actuators are connected in these pins. They must declared (software) is INPUT or OUTPUT.
• Voltage: 5V (HIGH) - 0V (LOW).
• Max. pin current: 40 mA. Max. total pins current: 200 mA.
1.2.1. Electronic components
What does ARDUNIO do with the information from sensors, touchscreens, etc.?
11th - 15th December 2017 UBORA First Design School - Nairobi 12
ENVIROMENT
Sensors:
• Temperature • Pressure • Light • Proximity • Acceleration • …
Actuators:
• Motors • Relays • Valves • Piezoelectrics • Diode led • …
ARDUINO ENVIROMENT INTERACTION
ELECTRONIC COMPONENTS
Pro
tob
oar
d
1.2.1. Electronic components
11th - 15th December 2017 UBORA First Design School - Nairobi 13
Transistors
Generate an output signal in response to an input signal
Functions: switch, amplifier, oscillator or rectifier
Diodes
Current circulation is only allowed in one direction
Many types:
Resistences
Correct resistance selection:
Cathode Anode
Current direction
base
collector
emitter
power
Great output signal
power
Low input signal Switch
application
Zener
Used as a constant voltage source
LED
(light emitting diode)
Photodiode
Allows reverse current
when light falls on it
Generic
Mostly used as rectifier for
converting AC to DC and
motor protection
I = V/R
1, 2 or 3 red or yellow LED (1,8-2,1V)
1.3. Environment interaction
What does ARDUNIO do with the information from sensors, touchscreens, etc.
11th - 15th December 2017 UBORA First Design School - Nairobi 14
ENVIROMENT
Sensors:
• Temperature • Pressure • Light • Proximity • Acceleration • …
Actuators:
• Motors • Relays • Valves • Piezoelectrics • Diode led • …
ARDUINO ENVIROMENT INTERACTION
1.3. Environment interaction
11th - 15th December 2017 UBORA First Design School - Nairobi 15
Movement
• Relays
• Valves
• Motors
• Etc.
Signal
• Display
• Leds
• Buzzer
• Etc.
Servomotor
or
Light, valve, etc.
open-close control
1.3.1. PWM outputs
11th - 15th December 2017 UBORA First Design School - Nairobi 16
¿Are there analogical outputs in ARDUINO? Digital PWM
PWM pins allows us to simulate an analog behaviour applying a continuous value. Arduino ONE board pins 3, 5, 6, 9, 10 ,11
How does it work?
Instead of a continuous signal
Pulses of square signals (490Hz)
We can varied pulse duration
Desired analog voltage = voltage average
Period
Pulse width
Average
Average
1.3.1. PWM outputs
11th - 15th December 2017 UBORA First Design School - Nairobi 17
EXAMPLE: How can we vary the DC electric motor speed?
Motor speed varies dependidng on the voltage applied to pin 9
Transistor with protection resistance
Motor con diodo de protección
External power supply
2. Arduino programming
1) Install the free software from arduino.cc/en/Main/Software
2) Conect Arduino board to the computer. Drivers will be installed automatically.
3) Open the software
11th - 15th December 2017 UBORA First Design School - Nairobi 18
Co
mp
ile
Up
load
New
Op
en
Save
code writing area
Software messages: errors and actions
2. Arduino programming
Arduino is programmed in C/C++
The algorithm consist of:
11th - 15th December 2017 UBORA First Design School - Nairobi 19
• Fundamental structures
• Variables y constants
• Mathematical, logical and Boolean operators
• Control structures (conditionals, cycles)
• Functions
2.1. Fundamental structures
11th - 15th December 2017 UBORA First Design School - Nairobi 20
Initial configuration code Only executed once
loop() is executed after setup () and keeps running until the Arduino is disconnected
Both functions are usually preceded by void These are functions without an output or result.
2.2. Variables, constants, mathematical, logical and Boolean operators
• Variables: Boolean open=true; int conta=5; char id=‘a’;
• Constants: const float pi = 3.14
• Mathematical operators: = , - , + , / ,…
• Logical operators: == , > , != , <= ,…
• Boolean operators: && (and) , II (or) ,…
11th - 15th December 2017 UBORA First Design School - Nairobi 21
2.3. Control structures
11th - 15th December 2017 UBORA First Design School - Nairobi 22
If
if (input<500) // action A else // action B
Switch/case
switch (var) case 1: // action A break; case 2: // action B break; default: // action B
For
for (int a=0; a>10; a++) // repeated action
While
while (a<10) // repeated action a++;
2.4. Functions
11th - 15th December 2017 UBORA First Design School - Nairobi 23
pinMode()
Configurates a pin PinMode(pin,mode)
pinMode(13, OUTPUT);
pinMode(a, INPUT);
digitalWrite()
Writes 1 or 0 in a digital pin digitalWrite(pin,state)
digitalWrite(13, HIGH);
digitalWrite(13, LOW);
digitalRead()
Reads 1 or 0 in a digital pin digitalRead(pin)
int a = digitalRead(13);
analogRead()
Reads an analog pin (0-1023) analogRead(pin)
int a = analogRead(A0);
analogWrite() PWM
Reads an analog value (0-255) analogWrite(pin, PWM value)
analogWrite(9, 134);
Digital functions Analytical functions
2.4. Functions
11th - 15th December 2017 UBORA First Design School - Nairobi 24
Advance
Random numbers
External interruptions
Time
Mathematical
Communication
3. Arduino programming with Matlab
Benefits of using MATLAB for Arduino programming:
• Read/write sensor data interactively without waiting for your code to compile
• Analyze your sensor data using pre-built Matlab functions for signal processing, machine learning, mathematical modeling, …
• Quickly visualize your data (MATLAB plot types)
Disadvantage:
• Speed data trasnmision
NOTES:
• Is our arduino board compatible? https://es.mathworks.com/hardware-support/arduino-matlab.html
• Only allowed Matlab 2014 and later versions
More info: https://es.mathworks.com/discovery/arduino-programming-matlab-simulink.html
11th - 15th December 2017 UBORA First Design School - Nairobi 25
3. Arduino programming with Matlab
Examples
• Mapping your surroundings using MATLAB and Arduino https://www.youtube.com/watch?v=iaF81W-l6Xg
11th - 15th December 2017 UBORA First Design School - Nairobi 26
Ultrasonic distance sensor
Servomotor
4. Practical studies
11th - 15th December 2017 UBORA First Design School - Nairobi 27
Case 1: Blink led L
Aim: Arduino software/conect introduction
Steps:
1. Open arduino software
2. Open “blink” example
3. Read the code and understand it
4. Select the board and the port.
5. Load the code on the Arduino board. Few seconds after the "uploaded“ message appears, we see the LED blinking
4. Practical studies
11th - 15th December 2017 UBORA First Design School - Nairobi 28
Case 2: Touch lamp