Tuesday, September 4, 2018

DC motor with arduno


    DC Motor

DC Motor

 A DC motor (Direct Current motor) DC motors generally have just two leads, one positive and one negative. If you connect this two leads directly to a battery, the motor will rotate. If you switch on, the motor will rotate in the opposite direction.


Warning − Do not drive the motor directly from Arduino board pins. This may damage the board. Use a driver Circuit or an IC.
- we can use relay instead of motor IC
We will divide this chapter into three parts −
  • Just make your motor spin
  • Control motor speed
  • Control the direction of the spin of DC motor
Components Required
You will need the following components −
  • 1x Arduino UNO r3
  • 1x PN2222 Transistor
  • 1x Small 6V DC Motor
  • 1x 1N4001 diode
  • 1x 270 Ω Resistor

motor connections with arduino -



code-

const int pwm = 2 ; //initializing pin 2 as pwmconst int in_1 = 8 ;const int in_2 = 9 ; //For providing logic to L298 IC to choose the direction of the DC motor void setup(){pinMode(pwm,OUTPUT) ; //we have to set PWM pin as outputpinMode(in_1,OUTPUT) ; //Logic pins are also set as outputpinMode(in_2,OUTPUT) ;} void loop(){//For Clock wise motion , in_1 = High , in_2 = Low digitalWrite(in_1,HIGH) ;digitalWrite(in_2,LOW) ;analogWrite(pwm,255) ; /*setting pwm of the motor to 255we can change the speed of rotaionby chaning pwm input but we are onlyusing arduino so we are using higestvalue to driver the motor */ //Clockwise for 3 secsdelay(3000) ; //For brakedigitalWrite(in_1,HIGH) ;digitalWrite(in_2,HIGH) ;delay(1000) ; //For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGHdigitalWrite(in_1,LOW) ;digitalWrite(in_2,HIGH) ;delay(3000) ; //For brakedigitalWrite(in_1,HIGH) ;digitalWrite(in_2,HIGH) ;delay(1000) ; }

Tech IOT Web Developer

No comments:

Post a Comment