Bluetooth controlled DC motor, BT module review

Bluetooth controlled DC motor, BT module review

This post will explain in detail how I control the speed and direction of the DC motor in the video below by bluetooth.

First, here is the list of items used in the experiment
L298 Double DC motor bidirectional speed controller
AVR AtTiny2313V microcontroller
Bluetooth module for wireless serial communication
12V DC motor

About the bluetooth module
I bought this module as I saw it spread out over several chinese online stores, and was interested in seeing how easy it really is to implement it in a project.

Basically it’s a bluetooth replacement for the Rx and Tx wires in standard serial applications. So if you bought two of these you can have two microcontrollers or computers communicate with each other wireless in the same way as you’d set up standard serial communication. Just hook these up at the Rx and Tx pins of your devices and provide 5v logic supply and ground and you’re ready to go. If you’re not satisfied with the standard setup of the module (name: linvor, PIN: 0000, baudrate: 9600) you can change those settings by sending it AT commands serially. For example, AT+PIN1234 will set pin code to 1234. The device responds with PIN1234 OK. To change baudrate you use the AT+BAUDx where x represents the baudrate number described in the manual. #4 is 9600 and #1 is 1200, for starters.

But you don’t need two of these to have fun! As you already know, many devices these days are equipped with bluetooth, best known is your mobile phone. For your mobile phone to send and receive serial commands via bluetooth, the hardware and software of your phone has to support the Serial Port Profile protocol (SPP). Your android device supports SPP, your iPhone does not. Next thing you’ll need is an android application which can send and (optional) receive bytes by SPP. I found the free software called Blue Control in the android market, and this is what I’ve used here. You can also choose to try the QkCtrl Serial BT which is rather expensive, but much more configurable. Remember to pair your phone with the module from the Wireless settings in android before trying to connect to it in the applications.

Click the photo to enlarge, it will show less grainy. To see the whole figure, please see my schematic for the L298 motor controller in my previous post.

I found the wake/power pin on the bluetooth module to be optional. The manual says it should be tied to ground when activated, and tied to Vcc to make it sleep. Maybe there’s a pull-down resistor on the module, because it works when floating the wake/power pin. There’s also a State output pin for a status LED, but there is already a red one on the module, so no need for an extra. The LED will blink at ~5Hz when not paired, and lit in a stable condition when successfully paired with another device AND the serial port is successfully occupied (when connected within the phones bluetooth application, se video).

Programming the AtTiny2313V
For this application we will use two peripherals on the microcontroller
– The Timer 0, for PWM signal to the motor controller
– The UART, for communicating with the bluetooth module (and thus, your mobile phone)

The microcontroller is interrupt driven, and changes to the control pins are only made when a byte is successfully received via the bluetooth serial module. The characters from the different button in the Blue Control application are pre-defined, will be using the up, down, left, right and center button, which are represented by the ASCII characters U, D, L, R and C. All other characters are ignored by the microcontroller. Please see the below source code which has helpful comments.

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 3686400UL //Defines clock speed
#define USART_BAUDRATE 9600 //Baudrate for serial comm.
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

//Serial com. Interrupt Service Routine (runs each time a byte is received)
ISR(USART_RX_vect)
{
	char ReceivedByte;
	ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module)
	UDR = ReceivedByte; // Echoes it back for fun

	switch (ReceivedByte) //Which ASCII character was received?
	{
		case 'U': 	if(OCR0A <= 244)
						OCR0A += 10;
					break; //Increase PWM duty cycle

		case 'C': 	PORTB |= 0b11;
					OCR0A = 0;
					break; //Break motor by raising both direction inputs, PWM duty cycle 0%

		case 'D': 	if(OCR0A >= 10)
						OCR0A -= 10;
					break; //Decrease PWM duty cycle

		case 'L': 	PORTB &= ~(1<<PIN0) & ~(1<<PIN1); //clear Dir-pins
					PORTB |= (1<<PIN0);
					break; //Change motor direction to left

		case 'R': 	PORTB &= ~(1<<PIN0) & ~(1<<PIN1); //clear Dir-pins
					PORTB |= (1<<PIN1);
					break; //Change motor direction to left

		default:	break; //Character unknown to my routine, discard character

	}

}

int main(void)
{
	//I/O Initialization
	DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs
	PORTB=0; //All initialized to 0

	//Timer0 initialization
	TCCR0A |= (1<<COM0A1) | (1<<WGM00); //Phase correct PWM mode
	TCCR0B |= (1<<CS00) | (1<<CS01); //div64
	OCR0A = 0; //Initialize PWM duty cycle to 0%

	//UART
	UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx
	UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
	UBRRL = BAUD_PRESCALE;			//Sets
	UBRRH = (BAUD_PRESCALE >> 8);	//baudrate registers
	UCSRB |= (1 << RXCIE); //Enable USART-interrupt

	sei(); //Enable global interrupt

	/* This program is completely interrupt driven, so nothing goes on in while loop*/
	while(1); //Never gets out from here!

	return 0; //Never reaches this point!
}

This article has 47 comments

  1. Hi, awesome work!

    I was wondering where you got the manual for the Bluetooth module? I was looking for it from the vendor pages, but couldn’t find it.

  2. Yo, Henrik, this is cool. I saw a dude made bluetooth car controlled by android phone, by uzzor2k, his page
    http://www.uzzors2k.4hv.org/index.php?page=blucar
    He built it using “old style bluetooth module” amd with h bridge stuffs to control dc motor and attiny 2313 to be the boss and to control a dc servo. I was looking for explanation of his project then came across here. Can u update this work using h bridge like him but with “modern style bt module”, man? And btw, u r as cool as him using attiny2313, kicking arduinos ass. I like the “hacking air” created by using attiny

    • Henrik Sandaker Palm
      Friday 28 October 2011, 11:03 pm

      This guy is using the exact same bluetooth module as I am, only mine is mounted on a breakout board. I use the L298 chip for motor control which has two TTL input level h-bridges inside. You can ready the intro in the post bluetooth controlled dc motor for info on integrated h-bridges and the L298

      • yo, bro, sorry for the noobish stuffs then, I am a noob in this field.
        Btw, man, uzzors2k somehow recommend using RF-BT0417C bluetooth module, as it was “ttl compatible”, and “doesnt need rs232 voltage level”. This was what I meant with “modern bluetooth module”. Well, what do u think, man? Do u mind to give some explanations, man? That uzzor dude has been too quiet with questions, man

        • Henrik Sandaker Palm
          Sunday 30 October 2011, 10:03 am

          rs232 voltage levels is +-12v, and is intended for communicating on a computers serial port. TTL levels are 0v to 5v, or 0v to 3.3v, and are used between embedded systems and microcontrollers. The module he spoke about must be intended for adding bluetooth capabilities to be used on a computers serial port, using SPP (see my post). The more convenient way of doing this is using a usb bluetooth dongle, and setting up a virtual serial port in software on your computer instead.

  3. hiiii
    Can I use same bluetooth modules for two differents ICs?
    i m using is RF-BT0417C bluetooth module.

    but problem is that in this i m using 2Ic which r same(ATtiny2313) with different coding. which r controlled by different ASCII character codes so can u suggest me that using this module can i control or connect two same ICS.

    • The bluetooth module you are using, I see nothing about an I2C connection, only half a USB and half SPI connection, which is weird. You really must use the serial connection, Rx and Tx, like I did.

      The rest of your post is a bit unclear… Can you try rephrasing?

      • ok
        i have no knowledge about coding and modification of coding.
        so as u know that i m trying to make forklifter.
        for moving the robot the coding is same with all the component as it is.
        but for lifting i m using other attiny2313 with other coding (i.e. coding of bluetooth car controlled by uzzor2k, in which forward and reverse direction coding will use as uplift and downlift purpose which has different ASCII A and W)
        so now problem is that can i use a single bluetooth module for both the Ic operation.

        • Henrik Sandaker Palm
          Sunday 11 December 2011, 4:23 pm

          I’m sorry that I can’t help you further. I recommend you buy a book on C programming, then one on embedded or AVR programming.

          • Hey henry… working on controlling a arduino powered legged robot with android.. can you help me with the android source…would be of great help to me.. your work is awesome man!!

          • Henrik Sandaker Palm
            Friday 13 January 2012, 11:26 am

            Hi,
            I haven’t done any Android programming. If your are to use Bluetooth, the app QkCtrl requires no programming and is easy to use. Thank you for the nice comments

  4. […] see this post Bluetooth controlled DC motor, BT module review for a more in-depth demonstration of the Bluetooth […]

  5. HIIII
    I had start working on this robot car my h-bridge is working proper but there is some problem in the ic and bluetooth connecton due to that i m not able to get output voltage at interrupt pins and enable pins to drive h-bridge.
    and my bluetooth pin is pair with 1234 so do i need to change baurdrate to 9600 and how can i change it
    plz help me…

  6. Dear Henrik,
    I am not able to compile theBluetooth controlled DC motor, BT module review code, would you kindly sent me the .HEX file.
    whenever you are available. Thanks.
    Frederick.

    • Henrik Sandaker Palm
      Friday 9 March 2012, 5:09 pm

      It compiles clean with no warnings, I copied the code from this site myself. What errors are you getting? Remember to set the correct microcontroller (attiny2313) under Project -> Configuration Options -> Device.

  7. can i use bluetooth module AUBTM-20 with same program and interfacing….
    please tell me..!

  8. can any one tell me the program for this module……

  9. can i use bluetooth module AUBTM-20 with same program and connection….?
    please tell me

  10. Hey!
    Great work man congratulations. I have a quick question for you, I am really interested in this bluetooth module to hook it up to my MSP430 Launchpad and I was wondering, if I could use this module in Master mode as in make it connect to another device, instead of me connecting to it? I would greatly appreciate your help.

    Anil

    • Henrik Sandaker Palm
      Thursday 29 March 2012, 1:22 am

      The processor on the module can be loaded with different firmwares, and cannot be used like like you describe as-is. But I know it`s capable, so keep searching the web for others who have hacked this or bought different versions. Good luck!

  11. An awesome work !I am a student, I need to use android platform to control a MCU’s robotic car (atmega128<<school orders me to use this MCU@@ ) with 2 servo motors and 4 DC motors (with L298 H-bridge). My linkage is used the bluetooth (I buy the UART bluetooth slave board (chip is csr BC417) , so I have some questions about the communication:
    1. If I use the this my MCU (atmega 128) to do a robotic car, is the programing is same as AtTiny2313V?
    2. my bluetooth slave board is different than yours, but can I use your code to activate the bluebooth ?
    3. I am a beginner of learning atmega128 ( I just learn 1 month@@) but I love it very much. Any suggestion to learn the programming and controling the MCU more effectively? Thx ~
    Hin Yeung

    • Henrik Sandaker Palm
      Thursday 12 April 2012, 3:58 pm

      1. Programming is the same, but it may differ on some register names. You will find out which register names are not valid for your mcu by reading the datasheet or simply trying to compile your code.
      2. Your bluetooth module looks to be the same as mine, I guess only minor differences. I don’t think this will be a problem.
      3. You might want to get a book, but you learn can learn very much by reading and understanding others code, and of course practicing and doing assignments.

  12. Hello,
    You have done a great work here seriously. I have one doubt for you. I want to use ATMega32 for doing the same project and as you know it has default frequency of 1MHz. I want to know how to set it to 3686400Hz as in your program. Are you doing it by using hardware of ATMega32 or are you using external crystal of 3686400Hz frequency.

    Please give me detail information as I am newbie in this field and this frequency thing really scares me out everytime.

    Your help is appreciated!!

    • Hello,
      One more thing I want to ask you. Please specify the model number for bluetooth module which you have used in your program. If I used some other RS232 bluetooth module for same program, will it work or some changes in program are necessary?

      Your help is appreciated!!

      • Henrik Sandaker Palm
        Saturday 12 May 2012, 12:37 pm

        There is a link to the product just below the video. I can not know how a random bluetooth module will work when I haven’t tried it? My guess is that it’s probably similar in use, but might need to be set up or configured first.

    • Henrik Sandaker Palm
      Saturday 12 May 2012, 12:34 pm

      You choose between internal or externals oscillators by setting the fuse bits. You can do this graphically in AVR Studio very easily. If you want a frequency different than the internal ones (1, 2, 4 or 8 MHz) you need to use external crystal or some other resonator and set the corresponding fuse bits for external oscillator. In the AVR Studio drop down list of oscillator fuse bit options it is clearly written what option fits your application.

  13. hi, i am trying to realize the same project by using Atmega128 and it is not working, maybe because the atmega128 has USART0 and USART1, can u please help?? thank u

  14. sir,
    i want to connect this linvor bluetooth modual to at89s52.so please can you tell how can i connect this module to my mcu

  15. where can i find exactly the android app that you use …i badly needed it,,thank you…

    • Henrik Sandaker Palm
      Tuesday 5 March 2013, 11:35 am

      It seems to be gone! Too bad, it was a great app. Please let me know if you find anything similar. It was called QkCtrl

  16. Hello! I am using EGBT 045MS as well, I’ve tried to configure it as a slave(BT connected to microcontroller but laptop as the “host”) then whenever I send “A”, the LED will light up. When I set the BT as Master then tried to send characters to the terminal, the output do not match what was programmed in the microcontroller. My setup is that the BT (configured as Master) is connected to PIC16f877A trying to send characters to my laptop with Bluetooth feature. What seems to be the problem? Please kindly help me. I’m so stuck with this problem :(

    • Henrik Sandaker Palm
      Wednesday 17 July 2013, 9:48 am

      You need to check your serial port communications with a cable before you start debugging the wireless interface. Are you sure the UART is correctly set up in the PIC? You should not need to put the BT module in master mode to send data back to the computer. Have you read this document thoroughly?

  17. Hello,

    My problem is this:

    I have used the RF-BT0417C module in a project, but I soldered it on the circuit without changing its name and password. It is connected to an ATmega32 microcontroller. I am wondering if there is any way to change its name and password by sending the commands from the microcontroller or if it is possible to do these changes wirelessly from my computer.

    Thank you very much

  18. Thank you. This is a great tutorial. Out of curiousity.  Why is your bluetooth module so large?

Leave a Reply