Line Follower Robot - Arduino Mega/uno - Very Fast Using Port Manipulation

4,659

39

2

Introduction: Line Follower Robot - Arduino Mega/uno - Very Fast Using Port Manipulation

About: Hi, this is Ali Shwaiheen. I`m here to share knowledge and experience gained in the field of Electrical Engineering and learning cool things you make. I`m enjoying being member of this community.

Line follower robot is a useful robot that is used in ware houses, industries, and stores etc, where it follows a dedicated path. The presented robot captures line position with IR Sensors. If the robot get off the line it will U turn and back to the line again. Also, it would stop while the designed spot is appeared.I used Direct port manipulation method (setting output pins directly) instead of using the Arduino default digitalWrite() functions. Using the port manipulation method in programming results in more space in HEX file and more speed while executingthe code.

For example:

Lets look at the blink example, this information is from the Arduino IDE

Sketch uses 928 bytes (2%) of program storage space. Maximum is 32,256 bytes.

Now using the following similar example (port manipulation )

<p>#include <br>#include 
 
void setup()
{
DDRB = B11111111;
}
void loop()
{
PORTB = PORTB | 0x20; // Writes PORTB5 low
_delay_ms(500);
PORTB = PORTB & 0xDF; // Writes PORTB5 high
_delay_ms(500);
}</p>

the above Sketch uses 488 bytes (1%) of program storage space. Maximum is 32,256 bytes.

See the figure above ( compare between the two similar sketches).

see a great introduction on https://www.arduino.cc/en/Reference/PortManipulat...

read more about Direct port manipulation, using the digital ports [tutorial part 3]

More about Bitwise maths and logic operators [tutorial part 4].

Step 1: Components Required

List of Components:

    1. 1pcs L298N motor driver.
    2. 1pcs ARDUINO MEGA2560 Controller Board. Or ARDUINO UNO
    3. 5pcs line tracking module.
    4. 4pcs motor.
    5. 4pcs wheels.
    6. Power Supply.
    7. 1pcs Robot Chassis.
    8. Connecting Wires.
    9. Sensor Shield. (optional). or Bread Board.

    Or you just a hole kit.

    Step 2: Purchasing and Assembling the Car Robot

    To make This Project doable for Every ages and make it easy as possible, I suggest to buy a hole kit from any supplier you like.

    For example you can buy this Kit from Amazon Elegoo EL-KIT-012

    you should buy extra IR sensors module from Amazon Infrared Black White Line Detection

    you can see Instructions for Assembly

    on ASSEMBLE ELEGOO SMART CAR ROBOT KIT V1.0

    or on ASSEMBLE ELEGOO ARDUINO ROBOT VERSION 2.0

    Remember: To buy extra IR sensors module If the kit comes with less than 5 IR sensor module.

    you should place the the additional IR sensor besides others and try to fix them at the same level so they look as figure above).

    You can use a glue gun as tool to fix the additional sensors.

    Note: All the five sensors must be identical because some give a different output signal while detecting the same color of lines. If you have already in hands a different type dont worry you just need to take the inverter of the output signal.

    let say A = 1| then Y=~A= 0. as we clearly deal with Boolean values. Another solution is change all sensors output signals from high to low manually and vise versa.

    Step 3: Getting Start: Study the Cases Testing It

    The detailed procedures as follow:

    • Give each Sensors a name depends on its potion on the car chassis:((see figure above )
      • Ssensor number (1) = LF1
      • Sensor number (2) = LF
      • Sensor number (3) = MID
      • Sensor number (4) = RT
      • Sensor number (5) = RT1
    • Connect The Five Sensors to the arduino mega or uno as follow: all Ground to Arduino Ground and Voltage inputs To the arduino +5V.
    • LF1 Output Signals To pin A0.
    • LF Output Signals To pin A1.
    • MID Output Signal To Pin A2.
    • RT Output Signals To pin A3.
    • RT1Output Signals To pin A4.

    • Use an electrical black tape to perform a straight lines on floor +a 90 degree turn.
    • Download the attached Test Program. (Testing.ino)
    • Connect the Arduino board to your computer and upload the sketch.
    • Open Serial monitor and start placing the car on the line and wire down all possible condition.

    Step 4: Test Results

    The serial monitor shows the results upon moving the line tracker sensor in a white background to a black line. The sensor module has a HIGH output when subjected to the black line and LOW output signal when subjected to others whit or light color. See Figures above


    I Figure out all the situations that i need for my designed path.which is when:

    • S1+ S2+S3 catch the line the car turn Fully left 90 degrees. Shows on Serial Monitor(11100).
    • S3+S4+S5 catch the line the car turn Fully Right 90 degrees. Shows on Serial Monitor(00111).
    • Olny S3 catch the line the car go straight. Shows on Serial Monitor(00100).
    • All Sensors Catch the line line the car stop. Shows on Serial Monitor(11111).
    • All Sensor out of the line the car make U Turn back to the line. Shows on Serial Monitor(00000).

    These are the basic cases above.

    • S1 catch the line the car turnslightly left. Shows on Serial Monitor(10000).
    • S2 catch the line the car turnslightly left. Shows on Serial Monitor(01000).
    • S4 catch the line the car go slightly right. Shows on Serial Monitor(00010).
    • S5 Catch the line the car goslightly right.Shows on Serial Monitor(0001).

    More possible case are found :

    • S1+S2 catch the line the car turn slightly left.. Shows on Serial Monitor(11000).
    • S2+S3 catch the line. the car Turn slightly left. Shows on serial Monitor(01100).
    • S3+S4 catch the line. the care turn slightly right. Shows on serial Monitor(00110).
    • S4+S5 catch the line the car turnslightly right.. Shows on Serial Monitor((00011).

    Two more un necessary cases: ( I Would include just in case happens)

    • S1+S2+S3+S4 catch the line. The car turn Fully left 90 degrees.(11110).
    • S2+S3+S4+S5 catch the line. The car turn Fully right 90 degrees.(01111).

    Obviously we have 15 cases that we need to teach the robot to handle.

    Note: Microcontroller Boards can be programmed direct by Port. Making a hole port as an input port or an output port.

    Each port has 8 bits can be used separately one at a time or just make all the port as an input or output and that what I am going to do next for programming.

    Se more Information

    See Also (How to control arduino pins from registers without digitalWrite and digitalRead)

    Step 5: Connecting and Wiring !!! (Direct Port Register)

    For Arduino Mega/uno see Port pin diagram above: for other board you may see the ports pin out and make change in connection only the sketch will work for sure, you just need to two different ports.

    Note the two left motor Should Connect parallel to each other then to the motor drive as one. The same for the right motors.

    • The Connection For Arduino Mega Left Motors First:
      • The forward direction connect to pin 52 - PB2
      • The back direction connect to pin 53 - PB0 // First bet at port B
    • The right side motors
      • The forward direction connect to pin 50 - PB3
      • the back direction connect to pin 51- PB1
    • The Sensors connection:
      • S1 to pin 22 - PA0 // First bet at port A
      • S2 to pin 23 - PA1
      • S3 to pin 24 - PA2
      • S4 to pin 25 - PA3
      • S5 to pin 26 - PA4 // The fifth bet at port A
    • The Connection For Arduino Uno Left Motors First:
      • The forward direction connect to pin 10 - PB2
      • The back direction connect to pin 8 - PB0 // First bet at port B
    • The right side motors
      • The forward direction connect to pin 11 - PB3
      • the back direction connect to pin 9- PB1
    • The Sensors connection.
    • S1 to pin A0 - PC0 // First bet at port C
      • S5 to pin A4- PC4 // The fifth bet at port C
      • S4 to pin A3 - PC3
      • S3 to pin A2 - PC2
      • S2 to pin A1- PC1

    Step 6: One More Step Before Coding

    Converting All binary Test Results To Decimal :

    AS Shown in serial monitor only 5 Bits, Its actually 8 bits but in my Testing sketch I only identify for 5 inputs (5 sensors). Any way, I mean when serial shows 10000-- its in fact 00010000 as a binary number and 16 as Decimal ((you just add three zeros on the left )). If you want to Include more cases or want to add more sensors you can use any number converter calculator that can find online: Example

    we have:

    • 00000 - case (0) // Note what inside the brackets is the converted binary number as decimal
    • 00001 - case(1)
    • 00010 - case(2)
    • 00100 - case(4)
    • 01000 - case(8)
    • 10000 - case(16)
    • 00011 - case(3)
    • 00110 - case(6)
    • 01100 - case(12)
    • 11000 - case(24)
    • 00111 - case(7)
    • 11100 - case(28)
    • 01111 - case(15)
    • 11110 - case(30)
    • 11111 - case(31)

    Step 7: Coding for Arduino Mega2560

    I Find a good pdf file explain the switch- case function that I`m using in programming see the attached file .

    <p>/* Line Follower robot - 5 sensors included/ direct port manipulation For Arduino Mega2560 created by: Ali Shwaiheen March 2018*/</p><p>#include  // header file to include input/output port
    int  main(void)   //main function
    {
    unsigned char z;
    DDRA = 0x00;  //set PORTA as input port  (DDRA = 0x00 DDRA=0b00000000) same command
    DDRB = 0xFF;  //set PORTB as outputport</p><p>while(1) // create infinite loop(repeat forver)</p><p>{
        z=PINA; // read port A
        switch (z) // make decision
    {
    case (28): // Fully left 90 degrees
    {
    PORTB=0x09 ; // wirte High to PB0 & PB3
    break;
    }
    case (30):// Fully left 90 degrees
    {
    PORTB=0x09;
    break;
    }
    case (7):// Fully right 90 degrees
    {
    PORTB=0X06; // wirte High to PB1 & PB2
    break;
    }
    case (15):// Fully right 90 degrees
    {
    PORTB=0x06;
    break;
    }
    case (8):// Slightly left. 
    {
    PORTB=0x02; // wirte High to PB1
    break;
    }
    case (12):// Slightly left.
    {
    PORTB=0x02;
    break;
    }
    case (16):// Slightly left.
    {
    PORTB=0x02;
    break;
    }
    case (24):// Slightly left.
    {
    PORTB=0x02;
    break;
    }
    case (1):// Slightly Right.
    {
    PORTB=0x08;// wirte High to PB3
    break;
    }
    case (2): // Slightly Right.
    {
    PORTB=0x08;
    break;
    }
    case (3):// Slightly Right.
    {
    PORTB=0x08;
    break;
    }
    case (6):// Slightly Right.
    {
    PORTB=0x08;
    break;
    }
    case (4): // Straight
    {
    PORTB=0x0A; // wirte High to PB1 & PB3
    break;
    }
    case (0):// U turn go back to the line
    {
    PORTB=0x09; // 09 is in hex numbers
    break;
    }
    case (31)://STOP 
    {
    PORTB=0x00; // Write Low to PB0 to PB7
    break;
    }
    }
    }
    return 0;
    }</p>

    Step 8: Vedio * Code for Arduino Uno

    Step 9: Thank You

    Thanks to all. All are welcome to correct me if somethings go wrong. Feel free to ask questions.

    All the time you spend reading my INSTRUCTABLES is appreciated.

    Have fun:)

    Share

      Recommendations

      • Creative Misuse Contest

        Creative Misuse Contest
      • Water Contest

        Water Contest
      • Metalworking Contest

        Metalworking Contest

      2 Discussions