Cintillo Institucional

wiki comunidad

Proyecto Hardware Libre Cenditel

Página Cenditel

wiki Cenditel

Plataforma de Desarrollo Colaborativo

Curso Sensibilización Hardware Libre (Unidad 002)

logohl.png logohl.png logohl.png

003/01 Actividad 005

El Servo Motor

La Señal PWM

Control de PWM con Arduino

Prácticas

==== Trabajo con servo motores con librería servo.h ====

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(7);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
} 

Sin Librería Arduino

Este nos permite nodificar la frecuencia de la PWM

int potpin = 0;  // analog pin used to connect the potentiometer
int motorpin=9;
//int val;    // variable to read the value from the analog pin
int sensorval;
float val;



void setup()
{
//  myservo.attach(7);  // attaches the servo on pin 9 to the servo object
pinMode (motorpin,OUTPUT);

  TCCR1A = 0x00;  // esto es para setear el timer en uno de los modos de
generación de PWM
  TCCR1B = 0x13; // esto setea el "prescaler" (N), de la siguiente manera:

  // TCCR1B=0x11 --> N=1;  TCCR1B=0x12 -->N=8;  TCCR1B=0x13 -->N=64, etc.
  // N, el famoso "prescaler", es un divisor del clock de la Arduino (16MHz)

  ICR1 = 3000; //  ICR1 es el valor máximo del analogWrite, lo que le
llaman el "TOP"

}

void loop()
{
  sensorval = analogRead(potpin);            // reads the value of the
potentiometer (value between 0 and 1023)
  val = map(sensorval, 0, 1023, 75, 360);     // scale it to use it with
the servo (value between 0 and 180)
//  myservo.write(val);                  // sets the servo position
according to the scaled value
  analogWrite (motorpin,val);
  delay(15);                           // waits for the servo to get there
}