lunes, 20 de junio de 2011

Voltaje Externo en Arduino



He realizado podido encender una luz que 9v, la idea es utilizar corriente externa para no utilizar la del arduino.

Para realizarlo solamente he utilizado un transitor 2n4401
(NTE 123AP según http://nte01.nteinc.com/nte/NTExRefSemiProd.nsf/$$Search?OpenForm)

Simplemente se conecta la base al pin del arduino, para que envié la señal, el Colector al motor o la luz y el Emisor a la tierra junto con el otro extremo de la batería, y el otro extremo del motor a la batería.

Bueno, aquí dejo una imagen.


Saludos.

domingo, 19 de junio de 2011

Manejar un motor DC con Arduino sin integrados

BASADO EN LA LIBRERÍA STEPPER.H DE ARDUINO

Para utilizar un motor dc con polaridad en un arduino sin utilizar ningún chip o integrado, se podría realizar de la siguiente forma:
(descarga estos archivos al final de esta entrada del blog)

1. Crear una carperta en las librerias que se llame MotorDC o como desee.

2. Buscar en las librerías del arduino dentro de la carpeta Stepper y copiarlos en MotorDC (básicamente son dos Stepper.h y Stepper.cpp)


3. Cambiar de nombre a los archivos por MotorDC.h y MotorDC.cpp respectivamente.

4. Abrir el archivo MotorDC.cpp y eliminar las siguientes líneas.

case 1: /* 11 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, HIGH);
break;
case 2: /* 10 */
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
break;
case 3: /* 00 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, LOW);
break;

y luego


digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 1: // 0110
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, HIGH);
digitalWrite(motor_pin_4, LOW);
break;
case 2: //0101
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);
break;
case 3: //1001
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
digitalWrite(motor_pin_3, LOW);
digitalWrite(motor_pin_4, HIGH);


6. Remplace todos los Stepper por MotorDC. Debería quedar de esta forma:

/*
MotorDC.cpp basado en:
Stepper.cpp - - MotorDC library for Wiring/Arduino - Version 0.4
*/

#include "MotorDC.h"

/*
* two-wire constructor.
* Sets which wires should control the motor.
*/

MotorDC::MotorDC(int number_of_steps, int motor_pin_1, int motor_pin_2)
{
this->step_number = 0; // which step the motor is on
this->speed = 0; // the motor speed, in revolutions per minute
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in ms of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor

// Arduino pins for the motor control connection:
this->motor_pin_1 = motor_pin_1;
this->motor_pin_2 = motor_pin_2;
// setup the pins on the microcontroller:
pinMode(this->motor_pin_1, OUTPUT);
pinMode(this->motor_pin_2, OUTPUT);

// When there are only 2 pins, set the other two to 0:
this->motor_pin_3 = 0;
this->motor_pin_4 = 0;
// pin_count is used by the stepMotor() method:
this->pin_count = 2;
}
/*
* constructor for four-pin version
* Sets which wires should control the motor.
*/
MotorDC::MotorDC(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
{
this->step_number = 0; // which step the motor is on
this->speed = 0; // the motor speed, in revolutions per minute
this->direction = 0; // motor direction
this->last_step_time = 0; // time stamp in ms of the last step taken
this->number_of_steps = number_of_steps; // total number of steps for this motor
// Arduino pins for the motor control connection:
this->motor_pin_1 = motor_pin_1;
this->motor_pin_2 = motor_pin_2;
this->motor_pin_3 = motor_pin_3;
this->motor_pin_4 = motor_pin_4;
// setup the pins on the microcontroller:
pinMode(this->motor_pin_1, OUTPUT);
pinMode(this->motor_pin_2, OUTPUT);
pinMode(this->motor_pin_3, OUTPUT);
pinMode(this->motor_pin_4, OUTPUT);
// pin_count is used by the stepMotor() method:
this->pin_count = 4;
}
/*
Sets the speed in revs per minute
*/
void MotorDC::setSpeed(long whatSpeed)
{
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
}
/*
Moves the motor steps_to_move steps. If the number is negative,
the motor moves in the reverse direction.
*/

void MotorDC::step(int steps_to_move)
{
int steps_left = abs(steps_to_move); // how many steps to take
// determine direction based on whether steps_to_mode is + or -:
if (steps_to_move > 0) {this->direction = 1;}
if (steps_to_move < 0) {this->direction = 0;}
// decrement the number of steps, moving one step each time:
while(steps_left > 0) {
// move only if the appropriate delay has passed:
if (millis() - this->last_step_time >= this->step_delay) {
// get the timeStamp of when you stepped:
this->last_step_time = millis();
// increment or decrement the step number,
// depending on direction:
if (this->direction == 1) {
this->step_number++;
if (this->step_number == this->number_of_steps) {
this->step_number = 0;
}
}
else {
if (this->step_number == 0) {
this->step_number = this->number_of_steps;
}
this->step_number--;
}
// decrement the steps left:
steps_left--;
// step the motor to step number 0, 1, 2, or 3:
stepMotor(this->step_number % 4);
}
}
}
/*
* Moves the motor forward or backwards.
*/

void MotorDC::stepMotor(int thisStep)
{
if (this->pin_count == 2) {
switch (thisStep) {
case 0: /* 01 */
digitalWrite(motor_pin_1, LOW);
digitalWrite(motor_pin_2, HIGH);
break;
}
}
if (this->pin_count == 4) {
switch (thisStep) {
case 0: // 1010
digitalWrite(motor_pin_1, HIGH);
digitalWrite(motor_pin_2, LOW);
break;
}
}
}
/*
version() returns the version of the library:
*/

int MotorDC::version(void)
{
return 4;
}

7. Con el archivo MotorDC.h simplemente reemplace el Stepper por MotorDC, debería quedar de esta forma:

/*
MotorDC.h basado en:
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
*/
// ensure this library description is only included once
#ifndef MotorDC_h
#define MotorDC_h
// library interface description
class MotorDC {
public:
// constructors:
MotorDC(int number_of_steps, int motor_pin_1, int motor_pin_2);
MotorDC(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
// speed setter method:
void setSpeed(long whatSpeed);
// mover method:
void step(int number_of_steps);
int version(void);
private:
void stepMotor(int this_step);
int direction; // Direction of rotation
int speed; // Speed in RPMs
unsigned long step_delay; // delay between steps, in ms, based on speed
int number_of_steps; // total number of steps this motor can take
int pin_count; // whether you're driving the motor with 2 or 4 pins
int step_number; // which step the motor is on
// motor pin numbers:
int motor_pin_1;
int motor_pin_2;
int motor_pin_3;
int motor_pin_4;
long last_step_time; // time stamp in ms of when the last step was taken
};
#endif


8. Abre el programa Arduino y verifica que se encuentre la librería MotorDC


9. Aquí un ejemplo de programa


#include MotorDC //poner simbolos

#define motorSteps 96

#define motorPin1 8
#define motorPin2 9
#define ledPin 13
#define enablePin 11

// initialize of the MotorDC library:
MotorDC myMotorDC1(motorSteps, motorPin1,motorPin2);
MotorDC myMotorDC2(motorSteps, motorPin2,motorPin1);
//crear dos inicializaciones, con los pines al reves para darle polaridad

void setup() {

myMotorDC1.setSpeed(5);
myMotorDC2.setSpeed(5);
//probar las velocidades, la mejor es 5

// Initialize the Serial port:
Serial.begin(9600);
}

void loop() {
// Step forward 100 steps:
Serial.println("Forward");

myMotorDC1.step(10);
//el motor va a girar de cuerdo a la cantidad de pasos

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
//aqui detiene el motor
delay(1000);

Serial.println("Backward");

myMotorDC2.step(-10);
//el motor va a girar de cuerdo a la cantidad de pasos

digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(1000);
//aqui detiene el motor
}


10. Ejemplo de conexión. Pin 8 y Pin 9



10. Aun falta por mejorar. Por ejemplo colocarle un while y un sensor de tacto, para poder manejar las rotaciones o el tiempo de rotación.

Descargue aquí los archivos y el ejemplo
Archivos MotorDC.zip





Espero que les sirva.