1 of 20

Penn State

Robotics Club

MOTORS + PUTTING IT ALL TOGETHER

Based on GitHub Tutorial by Dhruv Sringari

2 of 20

The Goal:

Create a robot that moves forward until it reaches a wall and stop before colliding.

We’ll do this by adding motors to what we’ve built in tutorials 1 and 2.

3 of 20

Motors

DC Motor

2 Wires

Bidirectional if attached backwards

Cannot be supplied with sufficient power from an Arduino or Pi

Watch this to learn how a DC motor works

Motor Driver

Controlled by signals sent from Arduino

Powered by a separate battery source

Can do variable motor speed

4 of 20

Motor Controller (L298N)

Motor Controller

  • This is the motor controller that we use in the lab
  • It is not available in TinkerCad, so we’ll use a different but similar motor controller

5 of 20

Start with our circuit from last week

Available here if you don’t have it:

https://www.tinkercad.com/things/gGnORvqfupr

6 of 20

Add battery and motor controller

5 V Power

9 V Power

Ground

7 of 20

Connect Motor 1

5 V Power

9 V Power

Ground

Speed Signal

Forward Signal

Backward Signal

8 of 20

Define pins for motor 1

Add this before the setup() function:

// Define pins for motor controller

const int MOTOR1_FORWARD = 12; // Input 1

const int MOTOR1_BACKWARD = 11; // Input 2

const int MOTOR1_SPEED = 5; // Enable 1 & 2

9 of 20

Do the same thing for motor 2

5 V Power

9 V Power

Ground

Speed Signal

Forward Signal

Backward Signal

10 of 20

Define pins for motor 2

// Define pins for motor controller

const int MOTOR1_FORWARD = 12; // Input 1

const int MOTOR1_BACKWARD = 11; // Input 2

const int MOTOR1_SPEED = 5; // Enable 1 & 2

const int MOTOR2_FORWARD = 3; // Input 3

const int MOTOR2_BACKWARD = 2; // Input 4

const int MOTOR2_SPEED = 6; // Enable 3 & 4

11 of 20

Final Circuit

5 V Power

9 V Power

Ground

Speed Signal

Forward Signal

Backward Signal

12 of 20

Write the code: Setup Function

Add this to our setup() function:

// Set speed for both motors

digitalWrite(MOTOR1_SPEED, HIGH);

digitalWrite(MOTOR2_SPEED, HIGH);

13 of 20

Write the code: Loop Function

Add the bolded lines to our loop() function:

if (distance < 20) { // If the distance is < 20cm:

// Turn on the LED

digitalWrite(LED_PIN, HIGH);

// Turn off motor 1 by setting both pins to low

digitalWrite(MOTOR1_FORWARD, LOW);

digitalWrite(MOTOR1_BACKWARD, LOW);

// Do the same for motor 2

digitalWrite(MOTOR2_FORWARD, LOW);

digitalWrite(MOTOR2_BACKWARD, LOW);

} else { // If the distance is not < 20cm

// Turn off the LED

digitalWrite(LED_PIN, LOW);

// Setting the forward pin to high and backward to low will make the motors rotate forward.

digitalWrite(MOTOR1_FORWARD, HIGH);

digitalWrite(MOTOR1_BACKWARD, LOW);

// Do the same for the right motor.

digitalWrite(MOTOR2_FORWARD, HIGH);

digitalWrite(MOTOR2_BACKWARD, LOW);

}

14 of 20

Your robot is complete!

  • This is all the code and electronics necessary to create a robot that can detect a wall and stop before colliding with it
  • The only remaining step would be to build the hardware
  • If we were to build this in real life, it would look like the robot to the right

15 of 20

PWM Control

  • But what if the robot is moving too fast?
  • We can adjust the speed of the motors using PWM (Pulse Width Modulation) control

  • Digital pins can only be set to two voltages. In the case of the Arduino, these are 0 V (low) or 5 V (high)
  • However, we can send more complicated data with digital pins by turning them on and off really quickly

16 of 20

PWM Control

  • When the signal is in the up position we provide power to the motor and when it's down we turn off the power.
  • The higher the percentage of the duty cycle the longer the signal is in the on state. If the signal is on for longer then so is the motor.
  • This is called Pulse-Width-Modulation or PWM, we are changing the "width" of the on signal to increase or decrease the motor speed.

17 of 20

PWM Control in Arduino

  • On the Arduino, pins marked with a ~ symbol are capable of PWM
  • Rather than setting these pins to HIGH and LOW with digitalWrite(), we can set them to any integer value from 0 to 255 with analogWrite()
  • 0 is completely off
  • 255 is completely on
  • For example, 128 would give us a duty cycle of about 50%

18 of 20

Slowing down the motors

To slow down the motors, replace the code in setup() where we set the motor speeds with:

// Set speed for both motors

analogWrite(MOTOR1_SPEED, 128);

analogWrite(MOTOR2_SPEED, 128);

19 of 20

Questions?

  • Don’t hesitate to ask me or any officer!
    • In meetings or via GroupMe direct message
  • You can find my finished circuit and code here:
  • If you’re looking for a recap, check out the write-up of these tutorials by Dhruv Sringari:
      • https://github.com/Penn-State-Robotics-Club/tutorials
      • Note: These tutorials were designed for use in person, so the type of motor controller and the code for the motor controller and distance sensors differ.

20 of 20