Sunteți pe pagina 1din 38

Microcontroladores

2016 C. Wexler 1
xkcd.com

2016 C. Wexler 2
Contacto:

Carlos Wexler, Ph.D.


Profesor de Fsica
Director de Estudios de Grado, Dept. de Fsica & Astronoma
Vice Presidente, Senado de la Escuela de Post Grado
University of Missouri

Consejero American Physical Society

Email: wexlerc@missouri.edu
2016 C. Wexler 3
Libros

$96 en Amazon.com

$22 en Amazon.com

2016 C. Wexler 4
Seguridad
Tomacorrientes: AC, 220 Vrms, 50 Hz,
I = V/R potencialmente muy peligroso (dolor, quemaduras, paro
cardiaco).
Mucha potencia disponible: daos materiales
Mayora de la electrnica : bajo voltaje: 1-30 V, DC o AC.
Generalmente seguro en piel seca (bateria de 9V en la leguna ay!).
Algunas fuentes pueden producir mucha corriente (no en la piel):
P = V.I puede ser alto. Quemaduras, daos materiales, etc.
Adentro de CRTs (TV, monitores, osciloscopios viejos): muy
alto voltaje (12-50 kV). Algo peligroso, doloroso aunque
generalemnte no letal.
Linease de transmision (100s of kV) o grandes instalaciones
industriales: KAPUT!
Electricidad esttica (San Luis!): miles de V! ( 1 kV/mm).
Molesto. PUEDE SER MORTAL! a la electrnica! (esp.
MOSFET, es decir casi todo lo digital).
2016 C. Wexler 5
Que vamos a asumir
Conocimiento bsico de electricidad:
Voltaje, Corriente, Ley de Ohm I = V/R
Eventualmente leer diagramas de circuitos
(curso de electronica)

2016 C. Wexler 6
Un par de cositas bsicas
Electronica digital:
1, verdadero, high: ~ 5V (o 3.3 V)
0, falso, low: ~ 0 V

Transistores
NPN (bipolar) N-Ch. Enh. MOSFET

I
Vent

Vent = 0 I = 0, Vent = 5V I Vbat/RL


2016 C. Wexler 7
resistors
Identificar Componentes

capacitors

diodes
LEDs

transistors

integrated circuits

2016 C. Wexler 8
Identificar Componentes
PHYSICS 4060 Advanced Physics Laboratory

Appendix
Resistor Color Codes
Most frequently used resistors are based on carbon films with power ratings of 1/8-1 Watt. Capacitors (ceramic, mylar)
(http://www.elexp.com/t_resist.htm)

10x103 pF = 10 nF
20%, 1KV breakdown

+
Capacitors (electrolytic)

2200 uF
25V breakdown
Ojo polaridad!

2016 C. Wexler 9
Breadboards
Internal Connections
Doblar a 90

2016 C. Wexler 10
Simuladores

2016 C. Wexler 11
2016 C. Wexler 12
2016 C. Wexler 13
2016 C. Wexler 14
2016 C. Wexler 15
Microcontrollers 101
Computer on a chip (ATmega328)
From under $1 to $100s
Memory:
RAM (Random Access): 2 kB, write (variables, data), resets if
power is turned off
FLASH: 32 kB, write 104 (program storage)
EEPROM (Electrically erasable programmable Read Only): 512 B,
write 105 (data storage)
Peripherals:
digital in/out lines (14+, 6 provide PWM)
analog to digital conversion (8, 10 bits, <1 Msps)
communication: serial UART1, SPI2, I2C3
programming: via serial (bootloader), via ICSP4
1Universal Asynchronous Receiver Transmitter, 2Serial Peripheral Interface, 3Inter Integrated Circuit,
4In-Circuit Serial Programming
2016 C. Wexler 16
2016 C. Wexler 17
2016 C. Wexler 18
Shields (cool thing! but not breadboardable)

2016 C. Wexler 19
USB (serial ICSP programming
communication, (loading bootloader)
programming
via bootloader, RESET*
XTAL, 16 MHz
power)

POWER IN
(6-20 V)
Activity LEDs POWER OUT
(power, RX, TX, D13) (regulated 5V, 3.3V)
2016 C. Wexler 20
Digital I/O ports
D0-D13 (~ = PWM) External INTERRUPTS
SPI (10, 11, serial communication
12, 13) RX (D0), TX (D1)

POWER IN
(6-20 V)

I2C (A4,A5) Analog In (A0-A7), also


Digital I/O (D14-D19)
2016 C. Wexler 21
USB-Serial adapter 5 V Voltage Regulator

WARNING:
Possible issues with FTDI USB-Serial adapters.
Pirate chips get bricked by some versions of Windows FTDI drivers.
Since it is impossible to know whether you have the real FTDI chip, avoid.
New nanos with CH340 chip advisable.
2016 C. Wexler 22
ATtiny85
Internals
(328
little
brother)

2016 C. Wexler 23
ATtiny LED Flasher

const int ledPin = 4; void loop() {


const int potPin = 3; int potReading = analogRead(potPin);
// 0 to 1023
void Setup() { digitalWrite(ledPin, HIGH);
pinMode (ledPin, OUTPUT); delay(potReading/2);
pinMode (potPin, INPUT); digitalWrite(ledPin, LOW);
} delay(potReading/2);
}
2016 C. Wexler 24
Arduino IDE (integrated development environment)

2016 C. Wexler 25
Reading a switch (digital input)

2016 C. Wexler 26
Reading Voltages (analog input)

const int tmp36Pin = 1;


void loop() {
void Setup() { int tmp36= analogRead(tmp36Pin );
pinMode (tmp36Pin , INPUT); // do something
} }

2016 C. Wexler 27
RGB Led with PWM
/* example 1.2 - fun with PWM and RGB LED - Created 07/04/2010 --- CC by-sa v3.0 Share the love!
By John Boxall --- http://tronixstuff.com */

int red = 9; // the pins for the LED


int green = 10;
int blue = 11;
int i = 0; // for loops
int j = 0;
void setup()
{
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop()
{
// first, cycle up each primary colour twice
for (j = 1; j < 6; j++)
{ // loop 5 times
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(red, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
analogWrite(red,0);
delay (20);
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(green, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
delay (20);
analogWrite(green,0);
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(blue, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
delay (20);
analogWrite(blue,0);
}
// psychadelic time
for (j = 1; j < 10000; j++)
{
analogWrite(red,random(255)); // set red at random brightness between 0 and 254
delay (random(10,31)); // wait for a random duration between 10 and 30 milliseconds
analogWrite(green,random(255));
delay (random(10,31));
analogWrite(blue,random(255));
delay (random(10,31));
}
} 2016 C. Wexler 28
RGB Led with PWM
/* example 1.2 - fun with PWM and RGB LED - Created 07/04/2010 --- CC by-sa
v3.0 Share the love!
By John Boxall --- http://tronixstuff.com */

int red = 9; // the pins for the LED


int green = 10;
int blue = 11;
int i = 0; // for loops
int j = 0;
void setup()
{
pinMode(red, OUTPUT); // tell Arduino LED is an output
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

2016 C. Wexler 29
void loop()
{
// first, cycle up each primary colour
for (j = 1; j < 6; j++)
{
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(red, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
analogWrite(red,0);
delay (20);
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(green, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
analogWrite(green,0);
for (i = 0; i < 255; i++)
{ // loop from 0 to 254 (fade in)
analogWrite(blue, i); // set the LED brightness
delay(20); // Wait 10ms because analogWrite isn't instant
}
analogWrite(blue,0);
}

2016 C. Wexler 30
// psychadelic time
for (j = 1; j < 10000; j++)
{
analogWrite(red,random(255)); // set red at random brightness between 0 and 254
delay (random(10,31)); // wait for a random duration between 10 and 30 milliseconds
analogWrite(green,random(255));
delay (random(10,31));
analogWrite(blue,random(255));
delay (random(10,31));
}
}

2016 C. Wexler 31
Managing large loads

Keep Ib < 10 mA

(Ib > Ic/)

Ib = 0 (cool!!!)

IRF540N can
manage > 30 A @
up to 40 V

2016 C. Wexler 32
Controlling power with PWM (pulse width modulation)

D3

const int motorPin = 3;


void loop() {
void Setup() { int motorPower = 128 // 0-255
pinMode (motorPin , OUTPUT); analogWrite(motorPin , motorPower);
} // do something
}
2016 C. Wexler 33
Bidirectional Motor Control (H-bridge)

2016 C. Wexler 34
Controlling a Servo
// Sweep
// by BARRAGAN <http://barraganstudio.com>

#include <Servo.h> // using our first LIBRARY!


Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9);
// attaches the servo on pin 9 to the servo object
}

void loop() {
for(pos = 0; pos < 180; pos += 1)
// goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos);
// tell servo to go to position in variable 'pos'
delay(15);
// waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1)
// goes from 180 degrees to 0 degrees
{
myservo.write(pos);
// tell servo to go to position in variable 'pos'
delay(15);
// waits 15ms for the servo to reach the position
}
} 2016 C. Wexler 35
Stepper Motor Control

2016 C. Wexler 36
Analog Outputs (DAC)
Some micros have an actual DAC
inside.

The 328 does not.

You can either use a DAC chip (e.g.,


we have 8-bit DAC0808 ICs)

or a R2R ladder for lower number of


bits.

4 bit DAC:

Vout = Vcc * (1/24)*( 23 D3 + 22 D2 + 21 D1 + 20 D0)


2016 C. Wexler 37
Tarea opcional (divertida)
Calcular la resistencia equivalente entre A y B:

Calcular la resistencia equivalente entre los puntos A & B (o


B & C) de esta red infinita de resistores de 1 .
(esto es bastante facil usar el principio de superposision; es muchisimo
mas dificil para otros pares, p. ej. A & C, A & D,)

C more resistors in
all directions
A B D

2016 C. Wexler 38

S-ar putea să vă placă și