Arduino to Motors
Arduino code to control an analog DC motor, regulated by a potentiometer
// Control an analog DC motor, regulated by a potentiometer
int analogInput = 0;
int motorSpeed = 0;
void setup(){
pinMode (3, OUTPUT);
}
void loop(){
analogInput = analogRead(A0);
motorSpeed = analogInput/4;
analogWrite (3, motorSpeed);
}
// Control a servo motor
#include
Servo myServo;
int analogInput = 0;
int motorPosition = 0;
void setup(){
myServo.attach(9);
}
void loop(){
analogInput = analogRead(A0);
motorPosition = map (analogInput, 0,1023, 0,180);
analogWrite (3, motorSpeed);
}
Breadboard diagram:
Make a servo motor turn back and forth:
#include
Servo myServo;
int analogInput = 0;
int motorPosition = 0;
void setup(){
myServo.attach(9);
}
void loop(){
//analogInput = analogRead(A0);
//motorPosition = map(analogInput, 0, 1023, 0, 180);
//myServo.write(motorPosition);
for(motorPosition = 0; motorPosition =1; motorPosition--){
myServo.write(motorPosition);
delay(10);
}
}
Comments Off on Arduino to Motors