Microcontrollers and stuff A blog by Derk Vedelaar

3Jan/167

Arduino/Bluetooth/Android controlled Knex car

When I was a kid I played a lot with Knex. Recently I found it again and tried to use the Cyber Knex motors and remote. Of course the batteries of the remote where empty and I had no new ones handy. Time to make my own.

The Cyber Knex kit has 3 motors. 2 of them are connected to the same battery pack, and one has it's own battery pack. They are connected using connectors to the main unit, which also has sound, lights, an on/off button and the remote control receiver. I decided to only use the motors, and create my own receiver using Bluetooth.

My step one: Create a Knex robot (I just made one from the book):

car

To get this working I needed to figure out the pinout of the motors I have. See the picture:

pinout

The picture is of the batterypack with only one motor, but I also added the pinout for the batterypack with 2 motors also in the picture. There is one pin I could not figure out what it did. Maybe it's just to make it fit into a standard connector. I don't know. I just connected the pins of the motor to an digital out pin of the arduino and that worked (don't forget to also connect the ground line to your arduino).

The connectors I am using are standard 2x3 connector cables and I used a file to cut off some off the sides to make it fit. If you file too much off the sides the top part of the connector will get loose. I just glued the connector back together.

Some tests showed that the motors start rotating after connecting it to the arduino. Now connecting the bluetooth module to it. I used an HC-05 module, which is a cheap module which you can get almost everywhere and I already had some, so maybe you have too. I always have problems when connecting these to the arduino and then reprogramming the arduino, so I choose to use some other pins than rx and tx, and use software serial to communicate.

This is where I started to worry about the other side of the system, the remote. I decided that I wanted to use my android phone with it so I needed to create an android app. I found this site: http://appinventor.mit.edu/ where they make it really easy to create something that I could use. It no-time I had an android app running on my phone. Long story short, go to the website, download my code, import it into the website and run it on your phone.

On the Arduino I was able to keep the code quite simple. You might need to change the pin numbers on the top:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX

char inData[20];
char inChar=-1;
byte index = 0;

int missilesPin = 10;
int missilesPinB = 11;
int motor2forward = 6;
int motor2back = 5;
int motor3forward = 9;
int motor3back = 3;
int headlight = 13;

void setup()
{
  mySerial.begin(9600);
  pinMode(missilesPin, OUTPUT);
  pinMode(missilesPinB, OUTPUT);
  pinMode(motor2forward, OUTPUT);
  pinMode(motor2back, OUTPUT);
  pinMode(motor3forward, OUTPUT);
  pinMode(motor3back, OUTPUT);
  pinMode(headlight, OUTPUT);

  digitalWrite(missilesPin, LOW);
  digitalWrite(missilesPinB, LOW);
  digitalWrite(motor2forward, LOW);
  digitalWrite(motor2back, LOW);
  digitalWrite(motor3forward, LOW);
  digitalWrite(motor3back, LOW);
  digitalWrite(headlight, LOW);
}

void loop() // run over and over
{
  while (mySerial.available() > 0)
  {
    if(index < 19)
    {
      inChar = mySerial.read();
      inData[index] = inChar;
      index++;
      inData[index] = '\0';
    }
  }

  if (index >= 3)
  {
//    if (!strcmp(inData, "Mu\n"))
    if (inData[1] == 'u')
    {
      digitalWrite(missilesPin, LOW);
      digitalWrite(motor2forward, LOW);
      digitalWrite(motor2back, LOW);
      digitalWrite(motor3forward, LOW);
      digitalWrite(motor3back, LOW);
    }
    
    if (!strcmp(inData, "Md\n"))
      digitalWrite(missilesPin, HIGH);

    if (!strcmp(inData, "Fd\n"))
    {
      digitalWrite(motor2forward, HIGH);
      digitalWrite(motor3forward, HIGH);
    }

    if (!strcmp(inData, "Bd\n"))
    {
      digitalWrite(motor2back, HIGH);
      digitalWrite(motor3back, HIGH);
    }

    if (!strcmp(inData, "Rd\n"))
    {
      digitalWrite(motor2forward, HIGH);
      digitalWrite(motor3back, HIGH);
    }

    if (!strcmp(inData, "Ld\n"))
    {
      digitalWrite(motor2back, HIGH);
      digitalWrite(motor3forward, HIGH);
    }

    if (!strcmp(inData, "H1\n"))
    {
      digitalWrite(headlight, HIGH);
      Serial.println("Headlight on");
    }

    if (!strcmp(inData, "H0\n"))
    {
      digitalWrite(headlight, LOW);
      Serial.println("Headlight off");
    }

    Serial.println(inData);
    index = 0;
  }
}

 

If you have the code running on your arduino and the app on your phone, you should be able to try it. Connect to your bluetooth module with your phone and then in the app, select the bluetooth module again. After you have done this the light will stop blinking on the module. It should now work!

I then decided that I wanted to solder it together to make it a nice module. There are many tutorials on how to create your own clone arduino and I am not going to cover that here. It's not that hard. Here are some pictures of what I came up with:

IMG_20160102_221119132 IMG_20160102_221155597

The hardest thing to solder was the usb socket for power. If you want to do the same consider removing the middle 3 pins with some pliers. Makes soldering those small pins easier.

Posted by Derk