Bluetooth Controlled Car

In this project I plan to control a robot via bluetooth using a serial bluetooth module the hc-05.
I firstly wanted to get familiar with changing the state of the robot with a hard wired serial to USB device. I built a simple circuit with three LED’s and code that has three different states; one state for each LED to be turned on, then a python script to send different characters via a USB to serial port.

Here is a circuit diagram of the system:


Here is a picture of the circuit:



Here is my code:

// Testing serial control with USB serial to dsPIC30F4011
// Written by Aron Horan
// 10 Dec 2013
#include <xc.h>
#include <libpic30.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

int main(void)
	int state = 0;
    char g, h, j, c; 
    TRISB = 0;
    // Configure UART
	U1BRG = 48;            // 38400 baud @ 30 MIPS
	U1MODEbits.UARTEN = 1; // Enable UART

		// Check if any characters were received via UART
        if (U1STAbits.URXDA == 1)     
            // Check that first character was 'f,g or h'
            c = U1RXREG;
            if (c == 'g') state = 1;
            else if (c == 'h') state = 2;
            else if (c == 'j') state = 3;

		//LED on RD0
		if (state == 1) 
			LATB = 0b001;
		//LED on RD1
		if (state == 2)
			LATB = 0b010;
		//LED on RD2
		if (state == 3)
			LATB = 0b100;
	return 0;

I firstly wanted to setup the python to send a char every three seconds, after I get that going I will change it so that it will take inputs from my keyboard.
I also am using a USB to serial port for the moment, once I get that up and running I will setup the bluetooth serial port and control remotely.

Here is my python code that sends a different char every three seconds:

import time
import serial

# configure the serial connections
ser = serial.Serial(
while True:

	print "g"
	print "h"
	print "j"

Here is a video of it in action:

Now that I have that working I have edited the python script to recieve inputs from the keyboard to change state.

Here is the new python code:

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
while True:

#print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
	# get keyboard input
	input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
	if input == 'exit':
		# send the character to the device
		# (note that I happend a \r\n carriage return and line feed to the 			characters - this is requested by my device)
		ser.write(input + '\r\n')
		out = ''
		# wait half a second before reading output (give device time 			 	     # to answer)
		while ser.inWaiting() > 0:
			out +=
		if out != '':
			print ">>" + out

The only thing is the I must press enter (return) for the char to be sent. My next stage I will overcome this problem. Also instead of using the USB to serial port I will be using a serial Bluetooth device so I can transmit wirelessly.

So I finally got it working.

Its wirelessly switching a few LEDs (wow!) but now I have the power to do a lot more with it.

I ended up using Tkinter to create a canvas with 3 buttons. Each button sends a different character to the PIC, changing it between three different states.

My C programm is exactly the same as above the only difference is the python script.

Here is my Python script:

import time
import serial
import Tkinter
# Define a keyboard callback function. This will be called
# every time a key is pressed anywhere in the window.
def key(event):
    print "Sending", repr(event.char)
# Callback function for the 'A' button
def a_press():
    print "Sending 'g'"
# Callback function for the 'B' button
def b_press():
    print "Sending 'h'"
# Callback function for the 'C' button
def c_press():
    print "Sending 'j'"
# Open first serial port
ser = serial.Serial(port='/dev/rfcomm0', baudrate = 9600, timeout=1)
# Create a Tk GUI window
root = Tkinter.Tk()

# create canvas with a width and height
Tkinter.Canvas(root, width=600, height=600)

# Add title to window
# Bind all keypresses to the "key" function (define above)
root.bind("<Key>", key)
# Create a few buttons and connect each one to a function
a = Tkinter.Button(root, text='A', command=a_press)
b = Tkinter.Button(root, text='B', command=b_press)
c = Tkinter.Button(root, text='C', command=c_press)
# Lay out buttons in the GUI
a.grid(row=0, column=0, sticky='EW')
b.grid(row=1, column=0, sticky='EW')
c.grid(row=2, column=0, sticky='EW')
root.columnconfigure(0, weight=1)
# Enter the Tkinter main event loop. The program will stay
# in this event loop until it's time to exit.
# Close serial port

Here is a video of it in action:

Having awful trouble with my bluetooth device, possible blew it somehow 😦 gonna order a new one and will get back to this project when it arrives.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s