DC motor speed control sketch

For learning motor speed control, the same breadboard setup from the first part of this chapter may be reused as is - simply add two more push buttons to the breadboard, as shown below. Remember that the button connected to digital I/O Pin 8 corresponds to Button 1 (the left most button on the breadboard) labelled in the following circuit:

Figure 3: DC motor speed control prototype

The preceding diagram should have become self-explanatory by now. Notice the two separate power sources, the common grounding technique and the concept of using a transistor (explained in the chapter for Day 2) and diode (explained in the chapter for Day 2). Build the circuit, load the sketch provided later in this chapter, and start running the prototype.

The following C sketch is designed to run the motor at full speed for 1 second and then drop the speed to 75% and run the motor at 75% speed for another 1 second, every time a push button is pressed.

Here is the DC motor speed control code:

//**********************************************************/ 
// Step-1: CONFIGURE VARIABLES
//**********************************************************/
int motorPin = 3; //this is a PWM capable pin
int buttonPin = 8; //button to start at low speed
int buttonPin2 = 7; //button to start at high speed
int buttonPin3 = 2; //button to stop
int buttonState = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
//**********************************************************/
// Step-2: INITIALIZE I/O PARAMETERS
//**********************************************************/
void setup()
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}
//**********************************************************/
// Step-3: MAIN PROGRAM
//**********************************************************/
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);

if(buttonState == HIGH)
{
Serial.println("button 1");
delay(1000);
analogWrite(motorPin, 170); //run at low speed
}
else if(buttonState2 == HIGH)
{
Serial.println("button 2");
delay(1000);
analogWrite(motorPin, 240); //run at high speed
}
else if(buttonState3 == HIGH)
{
Serial.println("button 3");
delay(1000);
analogWrite(motorPin, 0); //stop the motor
}
}

Let us understand the preceding program, step by step. As soon as the push button is pressed, the program reads the status via pin 2 and enters the if block and executes the analogWrite(motorPin, 255) line; sending a signal with a duty cycle of 100% on pin number 3, which in turn switches the transistor ON (for 100% of the time). This allows the current to flow across the motor and make it run at full speed.

When the program flow executes the analogWrite(motorPin, 192) line, it sends a signal with a duty cycle of 75% on pin number 3, which in turn switches the transistor ON (for 75% of the time). This allows the current to flow across the motor and make it run at 75% speed. This cycle is executed every time the button is pressed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.89.24