Skip to main content

Introduction

Learn about analogue inputs to make an even more sophisticated line following robot, that will smoothly follow any path.

  1. We just need the line sensor for now  - make sure your robot is setup like the picture.
    • We just need the line sensor for now - make sure your robot is setup like the picture.

  2. To make a smoother, better line follower, we need to use the line sensor in analogue mode. So far, we have been using it as a digital sensor - it can only be ON or OFF (1 or 0).
    • To make a smoother, better line follower, we need to use the line sensor in analogue mode.

    • So far, we have been using it as a digital sensor - it can only be ON or OFF (1 or 0).

    • Analogue inputs (and outputs) can have any value - think about the difference between a digital and an analogue clock

    • A digital clock must display a whole number of minutes

    • But on an analogue clock, the minute hand can be anywhere - even halfway between two minutes!

  3. Build the simple test program in the picture.
    • Build the simple test program in the picture.

    • Program your robot, and keep it plugged in.

    • Try moving the robot slowly from one side of the line to the other, whilst watching the speeds of the motors.

    • See how the change gradually as you approach the line?

  4. We can use this gradual change to smoothly change the amount the robot turns as it get further from the line!
    • We can use this gradual change to smoothly change the amount the robot turns as it get further from the line!

    • Add two new variables to the program inside the while True: loop, called l and r (left and right).

    • Let l = the analogue value of the left sensor (1), and r = the analogue value of the right sensor (0).

    • The analog_read_line function returns 100 if the sensor is completely off the line (on white), and 0 if the sensor is completely on the line (black).

  5. The larger the difference between l and r, the further the robot is from the line so the more we need to turn.
    • The larger the difference between l and r, the further the robot is from the line so the more we need to turn.

    • For example, if both sensors are on the line, we don't need to turn at all and l and r will have the same value.

    • Add a new variable called turn.

    • After getting the values of l and r, set turn equal to the difference between l and r.

  6. Then, add two lines of code like the picture, to set the speeds of the motors using the turn variable.
    • Then, add two lines of code like the picture, to set the speeds of the motors using the turn variable.

    • Do you understand how the code works? (hint: turn is positive when we need to turn right, and negative when we need to turn left)

  7. You might have noticed that while the new program is smooth, it isn't as fast as the old two sensor digital program - it might also struggle with the tighter turns.
    • You might have noticed that while the new program is smooth, it isn't as fast as the old two sensor digital program - it might also struggle with the tighter turns.

    • To make it faster, we need to make sure 1 wheel is always going 100% forwards, and then change the speed of the other wheel only based on how large turn is to follow the line.

    • Change your program so it looks like the picture - this will make sure 1 wheel is always going at 100%.

    • To make this work for tight turns, we need to multiply the turn variable to it has a bigger effect. Try it out with 3 to start with - you might need to adjust this depending on your exact robot setup, and how tight the turns are on the line.

    • Be sure to test it properly - try adjusting things until your program is 100% reliable.

  8. If you're feeling really advanced, add the Sparkle module back in and set the colours of the LEDs proportionally based on how far away from the line the robot is!
    • If you're feeling really advanced, add the Sparkle module back in and set the colours of the LEDs proportionally based on how far away from the line the robot is!

    • Your robot can also get lost and now has no way of finding the line again - try and add the code you wrote previously back in so the robot can't get lost, or at least stops if it loses the line completely.

Finish Line

Tech Camp

Member since: 10/03/2012

119 Guides authored

Team

Tech Camp Staff Member of Tech Camp Staff

6 Members

140 Guides authored