Analog Write

bulb, 4k wallpaper 1920x1080, beautiful wallpaper-5665770.jpg

In this lesson we will learn how to use the analog write function to turn on our motors and set their speed. We can use analog write function for our PWM[|] pins.


Estimated time: 7 minutes


You will learn about:

Analog Write

 Motor Function

Direction control

Speed Control

If Statement


Components Needed

Main Boardarduino, electronic, card-5170681.jpgUSB Cable
Arduinohand, reach, reaching-5961660.jpgBodymercedes-benz, car, clocks-1464852.jpg
Motorgears, bulb, innovation-1443730.jpg3 pieces/ M3 x 30 Screwsmegaphone, speaker, speak-2374502.jpg
14 pieces/ M3 Hex Nuthand, reach, reaching-5961660.jpg9 pieces/ M3 Lock Washertechnology, robot, futuristic-3940288.jpg
14 pieces/ M3 Flat Washercontroller, playstation, hand-852271.jpg4 pieces M3 nylon washersbridge, illuminated, multicoloured-1092256.jpg
6 pieces M3 x 8 Screwsimac, apple, mockup-606765.jpgScrew Driverspeech icon, voice, talking-2797263.jpg
Plierselectronics, circuit board, computer-1607250.jpg
Step 1:

Install Mainboard on the body [|] For that, you need to use M3 x 10 screws in the installation holes with Nylon washers touching the Mainboard, [|], then using flat washers touching body and nylon washers and lock washers after that and then hex nut.] using screw driver and pliers.

Step 2:

Install the motor on the right side of the body at the side M1 is placed on the MainBoard [|] For that, you need to use M3 x 30 screws with flat washers touching the motor body at one side and the robot body at the other side, then adding a lock washer and then the hex nut in the end, [|], we use the motor inside body method here.] using 3 M3 x 30 screws, 6 flat washers , 6 lock washers and 6 hex nuts.

Step 3:

Connect [|] For that, you need to place the Arduino on top of the Arduino pin headers [|], the USB port of it facing the edge of the board, and while you can see the access pins of the pinheader at both sides of the Arduino board, then push it in place to ensure the board is in place.] the Arduino to the Main board.

arduino, electronic, card-5170681.jpg
Step2:

Plug in the USB cable[|] the USB type B to Micro USB, which you can get from a cellphone charger also] to the Micro USB port of Arduino and then plug it to the USB port of the computer.

usb, logo, input-1773302.jpg
Step3:

Upload [|] Follow this] the following code to the Arduino.

 int M1A=3;
 int M1B=4;
int speed=50;
void setup() {

pinMode(M1A, OUTPUT);
pinMode(M1B, OUTPUT);

}
 
void loop() {

  // Forward
digitalWrite(M1B,LOW);
analogWrite(M1A,speed);  
delay(1000);

  // Backward
digitalWrite(M1B,HIGH);
analogWrite(M1A,speed);
delay(2000);

//Changing Speed
speed=speed+50;

//Keeping the speed within 50 to 255
if (speed>255)
{
speed=50;
}

}
Step 4:

Connect the motor wires to M1 Screw terminal using the screw driver. The red wire goes to The Terminal of M1 closer to Cellpack, the black wire goes to the terminal further from cellpack.

Step 5:

Install the Cellpack in its place [|] and connect the DC power jack Connector to female DC connector on the Mainboard.

Step 6:

Put the 3C and 4C Jumper Caps [|] in the connecting state so the two corresponding pins connect to each other.

Step 7:

Turn the Cellpack on [|] and check the movement of the motor, we have two increments, one lasting 1 seconds, motor running forward the other one 2 seconds, motor running backwards. You should notice the one second part starting slow and increasing speed, and the two seconds part running fast at first and slowing down.

Code Explanation
  • In lines 1 and 2, [|]
  • In line 3,[|] we define the integer value representing our speed value, which the initial value of it is equal to 50.] .
  • In line 4,[|] we start the void setup part of our code.] .
  • In lines 6 and 7,[|] we define our motor driver control pins as OUTPUT.] .
  • In line 9,[|] we close the void setup part of our code.] .
  • In line 11,[|] we start the void loop part of our code.] .
  • In line 13,[|] we signify the part of our code that makes the motor rotate in a direction that would move the robot forward.] .
  • In line 14,[|] we digital write control pin B, low or feed it with zero volts.] .
  • In line 15,[|] we analog write control pin A, with the value of speed, which for the start is 50, that will make the motor be fed with a voltage around 1.4 volts, because the potential difference is 1.4-0 volts.] .
  • In line 16,[|] we make the robot keep doing what it was doing before delay (rotating in the forward direction for 1000 milliseconds or 1 seconds.] .
  • In line 18,[|] we signify the part of our code that makes the motor rotate in a direction that would move the robot backwards.] .
  • In line 19,[|] we digital write control pin B, HIGH or feed it with maximum volts available to motor driver, in a fully charged Cellpack that will be around 7 volts.] .
  • In line 20,[|] we analog write control pin A, with the value of speed, which for the start is 50, that will make the motor be fed with a voltage around 5.6 volts, because the potential difference is 7-1.4 volts.] .
  • In line 21,[|] we make the robot keep doing what it was doing before delay (rotating in the forward direction for 2000 milliseconds or 2 seconds.] .
  • In line 24,[|] we add 50 to the value of speed and store it in the variable of speed.Like for the first time the robot running the loop code, from here on, the speed value is 100, until the next time that we change it.] .
  • In lines 27,28,29 and 30, [|]
  • In line 32,[|] we close the void loop part of our code.] .
Further Experimentation
  • Try swapping the pin numbers and see how it affects the direction of the rotation.
  • Try using higher and lower speed values and see how the motor will work and determine the lowest working speed value for your motors.
  • Try changing the delay values to see how fast the motor can change rotation.
Practical Usage For our Projects
  • We can use analog Write function for many occasions, like for 2WD and 4WD motors, to change their speed, rotation directions.
  • We can also use if statements in a lot of occasions from bipedal robots, line followers , robotic arms, gripper ,… to make our robots more sentient and autonomous.
Instagram
Facebook
LinkedIn
YouTube
X (Twitter)
Scroll to Top