Saturday 14 February 2015

Arduino DC Motor Driving

Arduino Motor Driving

Arduino and L293B

       We will make another simple project with Arduino. This project's name is Arduino motor driving! We will learn how to control a motor with Arduino. Firstly we will control the motor's speed by a potentiometer. Then we will learn what is the PWM and how to change the motor's speed by the PWM.

We need,
  • Potentiometer
  • An Arduino board
  • L293D Motor Driver
  • 9V Battery
to make this project. I used L293D motor driver and 5K potentiometer but you can use L293B motor driver and any other potentiometer. This is the sketch of the circuit.


The difference between L293D and L293B is the current limit. L293D's current limit is 0.5 A and L293B's current limit is 1A. Now, we can see the code.



void setup() {
//we arrange pins, which we will use, as output 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work. 11th pin is PWM pin. 
 we arranged it as HIGH to make it disabled. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
digitalWrite(11, HIGH);

}

       When we load this code to Arduino, we will arrange the speed of the motor with potentiometer.

       Now, we will change the motor direction with code. Do not use the potentiometer for this time. Directly connect the motor to the motor driver's output pin. Look at the sketch.





void setup() {
//we arrange pins, which we will use, as output 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work.

 11th pin is PWM pin. 
 we arranged it as HIGH to make it disabled. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
digitalWrite(11, HIGH);

// motor will work for 3 seconds 

delay(3000);

//we should let the motor to stop
digitalWrite(11, LOW);
delay(1000);


//we will change the 8th and 9th pins' values. then motor will change its direction 

digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(11,HIGH);

delay(3000);

}


Now, we will change the motor speed by changing the PWM value in the code. We will use a special function for this: " analogWrite() ". Let's see the code


void setup() {
//we arrange pins, which we will use, as output 
 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work.

 11th pin is PWM pin. 
 We will use analogWrite() function to arrange the motor's speed. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
analogWrite(11, 255);  //This function has a range from 0 to 255


}


Let's watch the video to see how it works. We finished that project. As an idea, you can control the motor with a button. We will make an other project in a short time. Keep in touch!

If you liked this post please subscribe it to reach more people! Thank you :)


Sunday 1 February 2015

Arduino Serial Port

Arduino Serial Port

UART/USART

       We will speak about Serial Communication on Arduino. Serial Communication means transmitting and receiving data on the same cable. To provide this we use UART (Universal Asynchronous Receiver Transmitter)/USART(Universal Synchronous Asynchronous Receiver Transmitter) communication units. Roughly, serial communication is the process to send data with starting bit, data bit, parity bit and stop bit. We can adjust the communication speed with the unit of bit speed which is called as BAUD RATE. Now, let's learn how to do it.
       Arduino lets us to communicate with a computer or any other device by using serial port. Actually, we use the serial port uncounsciously while loading the code to the Arduino. We can use serial port in different projects. We will make a simple application for this. Arduino presents us special codes to use for serial communication. Firstly, we should specify begining, baud rate and then the data which we will send or receive. Of course we want to see the results and Arduino has a screen to see them. We can see it by clicking on the burning glass on the main screen of Arduino IDE. It is on the upper right corner.



When we connected Arduino board to pc and clicked on the glass, this screen will be on.


It is possible to see the value of BAUD RATE on the lower right corner. We can adjust it from there. Pay attention BAUD RATE values on the screen and in the code must be same to send and receive data properly.

We will make an application to see the time on the serial port screen.



// we declare time variable to show the time
int time=0;

void setup() {

/* starting to serial communication use Serial.begin comand.
 The value which is between paranthesis, shows the baud rate */ 

Serial.begin(9600);

}

void loop() {
  
/* we use Serial.print command to have write what we want to see on the screen */
  
    Serial.print("Connection Time: "); 
  
/*Serial.println command has a small difference. it passes to next line after
ownself */

  
   Serial.println(time);
   time++;
  
  /* arduino uses milisecond as the time unit. 
 we use 1000 milisecond delay command for 1 second */
   delay(1000);

}

After loading the code to Arduino, we will see the connection time on the screen. We just added serial communication application to our projects. We will make higher level projects about this issue. Keep in touch!

If you liked this post please subscribe it to reach more people! Thank you :)