edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Hardi Kurnianto
Published

QRcode Following Robot

A vision-guided robot that reads QR codes, decides routes in real time, and autonomously navigates with Raspberry Pi and Arduino.

BeginnerShowcase (no instructions)5 hours61
QRcode Following Robot

Things used in this project

Story

Read more

Schematics

CIRCUIT

Code

Arduino

Arduino
/*
 QR Robot Arduino Controller
 Receive command from Raspberry Pi
*/

#define IN1 5
#define IN2 6
#define IN3 9
#define IN4 10

void setup()
{
    Serial.begin(115200);

    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);

    stopMotor();
}

void loop()
{
    if (Serial.available())
    {
        String cmd = Serial.readStringUntil('\n');
        cmd.trim();

        if (cmd == "FWD")
            forward();

        else if (cmd == "BACK")
            backward();

        else if (cmd == "LEFT")
            left();

        else if (cmd == "RIGHT")
            right();

        else if (cmd == "STOP")
            stopMotor();
    }
}

void forward()
{
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);

    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
}

void backward()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);

    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
}

void left()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);

    digitalWrite(IN3, HIGH);
    digitalWrite(IN4, LOW);
}

void right()
{
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);

    digitalWrite(IN3, LOW);
    digitalWrite(IN4, HIGH);
}

void stopMotor()
{
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);

    digitalWrite(IN3, LOW);
    digitalWrite(IN4, LOW);
}

Python

Python
import cv2
import serial
import time

# Arduino Serial
arduino = serial.Serial('/dev/ttyACM0',115200)
time.sleep(2)

detector = cv2.QRCodeDetector()

cap = cv2.VideoCapture(0)

lastCommand=""

while True:

    ret,frame = cap.read()

    if not ret:
        continue

    data,bbox,_ = detector.detectAndDecode(frame)

    if bbox is not None:

        if len(data)>0:

            command=data.upper()

            if command!=lastCommand:

                print("QR :",command)

                arduino.write((command+"\n").encode())

                lastCommand=command

    cv2.imshow("QR Robot",frame)

    key=cv2.waitKey(1)

    if key==27:
        break

cap.release()
cv2.destroyAllWindows()

Credits

Hardi Kurnianto
24 projects • 20 followers
Master student at Intelligent Control and Systems Engineering Department

Comments