Motors tutorial - Robosoc-Southampton/Tutorials GitHub Wiki

Welcome to week 2

This week we're putting together some robots from ~scratch. The task is to wire up and program a driving robot that can be controlled via Bluetooth, then go head-to-head with some other teams in races and challenges.

This is a bit more of a hands on tutorial so there's not as much content here. Feel free to take a look back at the Week 1 electronics tutorial and Week 1 programming tutorial for reference, and don't forget to ask for help if you get stuck.

There's example code for all of the components in this tutorials repository.

Wiring up the L298

The L298 is the motor controller that distributes power from the battery to the two motors. It will take in two wires connected to the battery, a 5V and ground wire to power the Arduino, wires on each side going to the motors, and also have 6 inputs from the Arduino.

L298 diagram

Stick to using the thicker wires for the 12V power just for good practice and as it's a bit clearer what they are for (power not data).

You'll want

  • The battery connector with switch screwed into the 12V input and ground
  • The Arduino connected to the 5V output and ground (connect the VIN pin on the Arduino to the 5V output and use normal wires)
  • The motors plugged in to each side of the L298. Which wire goes where doesn't really matter as it'll just reverse the direction of the motor, which can be fixed in the code.
  • The Arduino connected to the data pins of the L298 (6 of them). The ENA and ENB pins are PWM (marked with a ~ on the Arduino).

Wiring up a servo

Servos are a lot easier to wire up. You need a 5V input, ground, and a PWM data wire. Typically they're wired red (5V), orange (data) and brown (ground). Technically, you should be using some form of power regulator for the 5V, but we're lazy so just stick it in any 5V output on the Arduino 😛.

Programming the robot

Controlling the L298

The L298 is controlled with 3 wires per motor. There are 2 IN wires and one EN wire. The EN controls the speed and takes a value between 0 and 255 inclusive. The IN wires are digital (on or off) and decide which of the two outputs for the motor should be on. Basically, the output for each wire is INX * EN and INY * EN. This means, if both of the INs are on or off, the motor won't move (same voltage on each wire -> 0 voltage across the motor), so you want to put one on and one off at a time. You control the direction the motor spins in by turning on the appropriate IN.

Next steps

Take a look at the examples for how to program any of the components.