[SOLVED] Line Follower Java Issue


Hello there,

I’ve got a problem with the Line Follower in Java!
First of all, there is no Line Follower Class on the GitHub Site, so i’ve went ahead and tried to write my own “LineFollower” Class, using the information I got from reading the Line Follower C and Python Methods.

public class LineFollower {
    private final I2CDevice device;

    public LineFollower() throws IOException, InterruptedException, I2CFactory.UnsupportedBusNumberException{
        int busId;

        String type = SystemInfo.getBoardType().name();

        if (type.indexOf("ModelA") > 0) {
            busId = I2CBus.BUS_0;
        } else {
            busId = I2CBus.BUS_1;

        I2CBus bus = null;

        try {
            bus = I2CFactory.getInstance(busId);
        }catch (Exception e){
        device = bus.getDevice(0x06);


     * Copy Pasted Method from the Board class
     * @param bytes
     * @return
     * @throws IOException
    public int writeI2c(int... bytes) throws IOException {
        if (Gopigo.getInstance().isHalt()) {
        // Convert array: int[] to byte[]
        final ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
        for (int i = 0, len = bytes.length; i < len; i++) {
            byteBuffer.put((byte) bytes[i]);
        try {
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        return Statuses.OK;

     * Copy Pasted Method from the Board class
     * @param numberOfBytes
     * @return
     * @throws IOException
    public byte[] readI2c(int numberOfBytes) throws IOException {
        if (Gopigo.getInstance().isHalt()) {
        byte[] buffer = new byte[numberOfBytes];
        device.read(buffer, 0, buffer.length);
        return buffer;

    public int[] readSensor(){
        try {
            writeI2c(new int[]{1,3,0,0,0}); //
            int regSize = 10;

            byte[] rBuf = readI2c(regSize);
            int[] rintbuf = new int[rBuf.length];

            for(int j= 0; j < rBuf.length; j++){ //byte to unsigned "byte"
                rintbuf[j] = rBuf[j] & 0xFF;

            int[] returnArray = new int[5];
            for(int i = 0; i < 10; i = i+2){
                returnArray[i/2] = rintbuf[i]*256+rintbuf[i+1];
            return returnArray;
        } catch (IOException e) {
            return new int[]{-1,-1,-1,-1,-1};

For a darkblue underground I get the byte Array: 288, 216, 127, 201, 175
For a white underground I get the byte Array: 245, 175, 108, 192, 218
And for 2 sensors darkblue and 3 sensors white I get the byte Array: 294, 208, 166, 214, 209

As you can see, these values barely change.

I expect the problem to be in the readSensor() Method at the write method call with the int array: 1, 3, 0, 0, 0
and I need help, to find the problem as there are no documentations on the Line Follower I could read to get the necessary information i need.

Im lookin’ forward to hear from anyone

[Edit] Additionally I’ve got the Issue that the check_line_sensor.py just returns: Line sensor found, but without anything more, it stops right there.

Note, that I dont use the provides robot raspberry os, ill use a classic debian jessie destr.


Hi @FriedrichS,

First of all, can you confirm us that the line follower sensor works properly on your current distro?
You need to install the gopigo repo on your Raspberry Pi.

Here’s the terminal command that does the install of gopigo repo:

  • sudo curl https://raw.githubusercontent.com/DexterInd/Raspbian_For_Robots/master/upd_script/fetch_gopigo3.sh | bash

After it’s installed, reboot the Raspberry Pi and try out the following example program and compare the values with what you get:

I’m pretty sure everything is alright, but it doesn’t hurt checking before we deep-dive.

Second of all, could you provide all the Java code?
You seem to have given us just the source code for the line follower sensor.

Thank you!


Hello RobertLucian,

I’ve installed the gopigo repo as you said and restartet my pi (may I note that I use the GoPiGo2?)
After that I tried to run the basic_example.py but there wont happen anything, just a blank console!
It’s just like when I run the check_line_sensor.py, it says, that it found a Line sensor but it wont show me any values.

I’ve used the following code in my main method:

        gopigo = new Gopigo();
        //bridge = new RosBridge();
        //camera = getCamera();

        gopigoListener = new GopigoListenerClass();
        //bridge.connect("ws://localhost:9090", true);

        try {
            LineFollower lineFollower = new LineFollower();

            int[] pos = lineFollower.readSensor();
            for(int i : pos){
        } catch (IOException | I2CFactory.UnsupportedBusNumberException | InterruptedException e) {

[Edit] I’ve connected the Line Follower to the I2C Port on the Board

[Edit 2] When I debugged my Line Follower Class, the readSensor() method:

byte[] rBuf = readI2c(regSize);

would return negative values to the byte[]


@FriedrichS couple more questions:

When you run i2cdetect on the command line, what do you see? Can you post a screenshot?

Also, is there any way you can switch to a solid black line? We haven’t tried this on blue. It should work, but just to keep things really similar, it would be better to use black.


@JohnC What do you mean by “switch to a solid black line”?

[Edit] Yes I can switch to a solid black line, but the issue, right now is, that neither my code nor the provided python code on Github would work


Hi @FriedrichS ,

1st thing: I think you need to tell us what happened between the moment when the line follower worked and when it didn’t.
Because it seems like there’s something missing.

2nd thing: I would stick with a black line. I remember testing it on a dark brown, dark blue colors and it wasn’t giving me anything satisfactory. After we get it to work again, please try it with a black line.

3rd thing: When you get a list of negative numbers from readI2c func, more specifically a list of 5 elements with values set to -1, this means your Raspberry Pi isn’t communicating with the line follower. This issue is extremely similar with what we’ve encountered before.
In a terminal, can you type uname -a and tell us what is the version of your kernel?

That’s all for now.

Thank you!



1st: It never worked for me, i thought my Java Code were the problem, but it seems like Dexter Industries provided Python code wont work either.
2st: Okay, I’ll stick with a black line
3st: “Linux raspberrypi 4.9.24+ #993 Wed Apr 26 17:56:54 BST 2017 armv6l GNU/Linux”

[Edit] im not getting an Array filled with "-1"
The latest Array I got were: [1,-96,1,120,1,87,1,-95,1,-55]


Hi @FriedrichS,

This is a common issue we have.
That version of the kernel has an issue, so we need to rollback our version to 4.4.50-v7+.

In order to do this, please enter the following command in your Raspberry Pi:

  • sudo rpi-update 52241088c1da59a359110d39c1875cda56496764

After you do this, please try our Python example again and tell us how it went.

Thank you!



That worked for me and the Java code is working too!

Thank you very much


Hi @FriedrichS,

I’m glad it solved out.
I’m closing the thread now.

Thank you!

closed #11