> > |
META TOPICPARENT |
name="DCMotorControl" |
Report on the first 6 weeks of the DC motor project
Week 1
Installing the ubuntu 16.04 OS as dual boot alongside windows 10
Installing the Arduino software on the ubuntu 16.04.
Week 2
Taking up a piece of the Arduino Project (DC Motors)
Doing some basic coding practice in Arduino.
Basic Structures: Setup () is where the initialization Step. Loop () is where the codes run over and over again.
‘If …else’ Statements
For loops Syntax: for (initialization; control; increment/decrement) { //statement block
}
HIGH / LOW: These are constants used to signify 0V or 5V (logic 0 or 1) when writing or reading from digital pins.
Week 3
Control a DC Motor using the Arduino
I went online (circuits.io) and did some simulation, it worked alright and generated some codes for me to run with the Arduino software.
I also did some readings on how to use and connect the various components on the Arduino breadboard with any flavor of the Arduinos.
L298N Motor Controller How to control the forward/Backward on the Motor
A direct current, or DC, motor is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.
Components : Arduino UNO, Jumper Wires, L298N Motor Controller, DC Motor, 9 volts’ battery.
|
|
|
9V battery |
DC motor |
Arduino Uno |
|
|
|
L298N motor controller |
USB programming cable |
breadboard cables |
How to do with these components? With DC motors you can do amazing things. Controlling DC Motors with Arduino is an important task. We can control DC Motor with Arduino by connecting L298N Motor controller to Arduino.
L L298N Pinout is:
- DC motor 1 “+” or stepper motor A+
- DC motor 1 “-” or stepper motor A-
-
12V jumper – remove this if using a supply voltage greater than 12V DC. This enables power to the onboard 5V regulator
-
Connect your motor supply voltage here, maximum of 35V DC. Remove 12V jumper if >12V DC
-
GND
-
5V output if 12V jumper in place, ideal for powering your Arduino (etc)
-
DC motor 1 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control.
-
IN1
-
IN2
-
IN3
-
IN4
-
DC motor 2 enable jumper. Leave this in place when using a stepper motor. Connect to PWM output for DC motor speed control
-
DC motor 2 “+” or stepper motor B+
-
DC motor 2 “-” or stepper motor B-
The code
This code consists of two demos.
int enB = 5; int enA = 10;/////For enabling motor and giving rotation speed hence pwm pins are best for this purpose. int in1 = 9;////Digital Pins are used because we only need to int in2 = 8;////give signal to motor whether to rotate or not. void setup () { // set all the motor control pins to outputs pinMode(enA, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
} void demoOne () { // this function will run the motors in both directions at a fixed speed // turn on motor A
digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enA, 200); analogWrite(enB, 200); delay(2000);
// now change motor directions digitalWrite(in1, LOW); digitalWrite(in2, HIGH); delay(2000);
// now turn off motor digitalWrite(in1, LOW); digitalWrite(in2, LOW); } void demoTwo() { digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
for (int i = 0; i < 256; i++) { analogWrite(enA, i); delay(20);
}
// decelerate from maximum speed to zero
for (int i = 255; i >= 0; –i) { analogWrite(enA, i); delay(20); }
// now turn off motors digitalWrite(in1, LOW); digitalWrite(in2, LOW); }
void loop() { demoOne(); delay(1000); demoTwo(); delay(1000); }
What is PWM and how does it work
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling brightness of the LED.
Week 4
control a DC motor via a push button which will make the DC motor go in the forward, reverse or stop depending on the number of pushes.
Her e's a picture of the schematic of the setup. My question is what is the code or function that I need to put in in order to make it go forward, reverse, and stop? And no involvement of a h bridge.
Sample Codes
if (MotorMode = 0) { // initial mode digitalWrite(1, LOW); // Motor in initial state = off } if (MotorMode 1) { // Motor in the forward direction digitalWrite(2, HIGH); // turn direction relay on for forward direction digitalWrite(1, HIGH); // turn motor voltage on } if (MotorMode 2) { // Motor stops digitalWrite(1, LOW); //turn motor voltage off } if (MotorMode = 3) { // Motor goes in the reverse direction digitalWrite(2, LOW); //turn direction relay off for reverse direction digitalWrite(1, HIGH); // turn motor voltage on } } Be sure you have a wire from the 9 volt supply negative terminal to the Arduino ground pin, it's required.
Reference: forum.arduino.cc/index.php?topic=73095.0
Week 5 and 6
By connecting an L298 bridge IC to an Arduino, you can control a DC motor.
A direct current, or DC, motor is the most common type of motor. DC motors normally have just two leads, one positive and one negative. If you connect these two leads directly to a battery, the motor will rotate. If you switch the leads, the motor will rotate in the opposite direction.
To control the direction of the spin of DC motor, without changing the way that the leads are connected, you can use a circuit called an H-Bridge. An H bridge is an electronic circuit that can drive the motor in both directions. H-bridges are used in many different applications, one of the most common being to control motors in robots. It is called an H-bridge because it uses four transistors connected in such a way that the schematic diagram looks like an "H."
You can use discrete transistors to make this circuit, but for this tutorial, we will be using the L298N H-Bridge IC. The L298 can control the speed and direction of DC motors and stepper motors and can control two motors simultaneously. Its current rating is 2A for each motor. At these currents, however, you will need to use heat sinks.
L298 Pinout (top view)
Hardware Required
-
1 x L298 bridge IC
-
1 x DC motor
-
1 x Arduino Mega2560
-
1 x breadboard
-
10 x jumper wires
The schematic above shows how to connect the L298N IC to control two motors. There are three input pins for each motor, including Input1 (IN1), Input2 (IN2), and Enable1 (EN1) for Motor1 and Input3, Input4, and Enable2 for Motor2.
Since we will be controlling only one motor in this tutorial, we will connect the Arduino to IN1 (pin 5), IN2 (pin 7), and Enable1 (pin 6) of the L298N IC. Pins 5 and 7 are digital, i.e. ON or OFF inputs, while pin 6 needs a pulse-width modulated (PWM) signal to control the motor speed.
The following table shows which direction the motor will turn based on the digital values of IN1 and IN2.
IN1
|
IN2
|
MOTOR
| | |
BRAKE
|
1
| |
FORWARD
| |
1
|
BACKWARD
|
1
|
1
|
BRAKE |
IN1 pin of the L298N IC is connected to pin 8 of the Arduino while IN2 is connected to pin 9. These two digital pins of Arduino control the direction of the motor. The EN A pin of IC is connected to the PWM pin 2 of Arduino. This will control the speed of the motor.
To set the values of Arduino pins 8 and 9, we will use the digitalWrite () function, and to set the value of pin 2, we will use the using analogWrite () function.
Below is a photo of the set up.
Code
const int pwm = 2; //initializing pin 2 as pwm
const 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 output
pinMode(in_1, OUTPUT); //Logic pins are also set as output
pinMode (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 255
we can change the speed of rotaion
by chaning pwm input but we are only
using arduino so we are using higest
value to driver the motor */
//Clockwise for 3 secs
delay(3000);
//For brake
digitalWrite(in_1, HIGH) ;
digitalWrite(in_2, HIGH);
delay(1000);
//For Anti Clock-wise motion - IN_1 = LOW , IN_2 = HIGH
digitalWrite(in_1, LOW);
digitalWrite(in_2, HIGH);
delay(3000);
//For brake
digitalWrite(in_1, HIGH);
digitalWrite(in_2, HIGH);
delay(1000);
}
Setup
-
Connect 5V and ground of the IC to 5V and ground of Arduino.
-
Connect the motor to pins 2 and 3 of the IC.
-
Connect IN1 of the IC to pin 8 of Arduino.
-
Connect IN2 of the IC to pin 9 of Arduino.
-
Connect EN1 of IC to pin 2 of Arduino.
-
Connect SENS A pin of IC to the ground.
-
Connect the Arduino using Arduino USB cable and upload the program to the Arduino using Arduino IDE software.
-
Provide power to the Arduino board using power supply, battery or USB cable.
The motor should now run first in the clockwise (CW) direction for 3 seconds and then counter-clockwise (CCW) for 3 seconds.
-- Uli Raich - 2017-05-03
Comments
|