The DC motor comes with my starter kit is integrated with the LC9110 motor control board. In this exercise, I am going to make the motor moves clockwise then anti-clockwise at the speed read from the serial input.
The controller has four pins:
VCC - connected to 5v
GND - connected to ground
IA - connected to pin 9 to control speed in clockwise direction
IB - connected to pin 8 to control speed in anti-clockwise direction
/*
L9110 motor driver controlling a small DC motor
*/
const int IA = 9; // pin 9 connected to pin IA
const int IB = 8; // pin 8 connected to pin IB
byte speed = 255; // change this (0-255) to control the speed of the motor
void setup() {
pinMode(IA, OUTPUT); // set pins to output
pinMode(IB, OUTPUT);
Serial.begin(9600);
while (!Serial);
Serial.println("Speed 0 to 255");
}
void loop() {
if (Serial.available()) {
speed = Serial.parseInt();
Serial.print("Set speed to ");
Serial.println(speed);
}
forward();
delay(1000);
backward();
delay(1000);
}
void backward()
{
analogWrite(IA, 0);
analogWrite(IB, speed);
}
void forward()
{
analogWrite(IA, speed);
analogWrite(IB, 0);
}
沒有留言:
張貼留言