Friday, May 24, 2013

Tuorial: Attiny84 / 85 and Bluetooth 

Tuorial: Attiny84 / 85 and Bluetooth

What This Tutorial Covers

This tutorial is an expansion / continuation of the previous Bluetooth tutorial. In other words I will assume you are familiar with arduino and simple Bluetooth concepts. Also, I assume you know how to program an Attiny via arduino as an ISP or via a standalone programmer. I'm not going to be going over the code in detail unless it is something I didn't cover in the previous tutorial.

In this tutorial we will learn how to connect a Bluetooth module to an Attiny and how to make them talk! Exciting isint it? This will allow you to control a shrinkified arduino project wirelessly via Bluetooth from another arduino bluetooth enabled device, via a computer or even via a phone. In this tutorial I will cover how to control your Bluetooth enabled Attiny via a computer, but soon I will be doing a tutorial on how to accomplish this via an iPhone.


What You Need

For this tutorial you will need a few things, here is the list:
- Attiny84 or 85
- Bluetooth module
- a bread board and some wires

Identify your Bluetooth

The first thing you have to do is check your hardware and find out which pin means what. This is not difficult and either the manufacturer or the retailer which you have purchased the module from should provide you with a schematic. Here is the module I'm using the HC-05 Bluetooth Transceiver:


You can get it from here from NYPLATFORM on ebay. ($11)

Set Up

Here are two pictures of the setup:




I know its not clear so I will run through it in more detail in text. The Bluetooth module I'am using has 6 pins. I'm using only 4 of them. The TX, RX, 5V, and GROUND. This is all you need to run the Bluetooth and make it work. Some other features are excluded due to this but I will leave it up to you to discover them.

OK, first the simple stuff that you should be familiar with already. In the code notice that I initiate "int led = 4" pin 4 as "led", then I set it up for output and turn it on. We will be using this led to verify connection with our computer via Putty. In other words we will control the led via Bluetooth and make it turn on and off. As mentioned before this led is connected to Attiny pin 4, in my setup I'm using a resistor (should be 330ohm) between the Attiny and the led as I do not want to fry the led.

Now for connecting the Bluetooth with the Attiny. This is simpler that you would have ever thought, all you need is two wires or jumpers that connect the RX pin with pin 1 on the Attiny and connect the TX pn with pin 2 on the Attiny. Now make sure everything has 5V connected to it, the Bluetooth module and the Attiny.

NOTE: some Bluetooth modules could be using a different Voltage!!!

Programming

Hold on there! Before we start programming there is one thing you have to make sure of. In order for this to work properly as I found out you have to burn the 8mhz boot loader for the Attiny! Google is your friend on this one.

Now, since I have gone through most of the code in my previous tutorial I will not be detailed on this. The only part that I will make remarks about is the new code that controls the led.

/*
This code will run the bluetooth as slave
pressing 1 turns on led 4
pressing 0 turns off led 4
*/
 
#include    //Software Serial Port
#define RxD 1
#define TxD 2

#define DEBUG_ENABLED  1
 
SoftwareSerial blueToothSerial(RxD,TxD);

int led = 4;
 
void setup() 
{ 
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  setupBlueToothConnection();
  
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH);
 
} 
 
void loop() 
{ 
  char recvChar;
  while(1){
    //check if there's any data sent from the remote bluetooth shield
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      
        if(recvChar == '1')
          digitalWrite(led,HIGH);  
       
        else
          digitalWrite(led,LOW); 
    }
  }
} 
 
void setupBlueToothConnection()
{
  blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=HC-05\r\n"); //set the bluetooth name as "HC-05"
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  
  delay(2000); // This delay is required.
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
  blueToothSerial.print("bluetooth connected!\n");
  
  delay(2000); // This delay is required.
  blueToothSerial.flush();
}

The only new code that you should see is the following:
void loop() 
{ 
  char recvChar;
  while(1){
    //check if there's any data sent from the remote bluetooth shield
    if(blueToothSerial.available()){
      recvChar = blueToothSerial.read();
      
        if(recvChar == '1')
          digitalWrite(led,HIGH);  
       
        else
          digitalWrite(led,LOW); 
    }
  }
} 

This is our loop. We do this 8mhz per second. Lets take a look at it and what it does. It is not very complicated and just looking at it you should be able to figure it out. Skipping to the first if statement, we are checking if there is any data that has been transmitted from the master device. Then, if there was we write it into a char. Now the char should be holding whatever value you have passed to it. In this example I choose "1" and any. "1" will turn on the led and any other will turn it off. Now in the second if statement we are comparing the char to "1" and if they match then we turn the led on. If they don't match we turn it off. So, as long as the connection is working, you should be able to turn off the led that is turned on at initialization.

Getting it All Connected

Now, power everything on. If you are using the same module as I am, you should see a red blinking led. This is good, your Bluetooth device is ready for pairing. Open up your computers Bluetooth software, find HC-05 and connect to it. This device uses a 4 digit pairing code. The default is "1234", type it in and sync them together. Open up device manager in control panel note the COM port that the Bluetooth is using, if there is more than one you will have to try them all if your unlucky. Open up putty select COM connection type in the COM# and leave the bound rate at 9600, this is what we used in the code and this is what worked best for this device.
If putty opens up a new session (black window) with the text displaying "Bluetooth Connected" you are in business. Press any key and the led should turn off, press "1" and the led should turn on. Congratulations!! You have established a bluetooth connection using an Attiny.

Comments and Questions welcome!
Thanks for reading.

21 comments:

  1. Great tutorial!!, simple to follow and easy to understand ;)

    ReplyDelete
  2. Do you know how to pwm a led? Not just a fade but controlable with a slider thru an app...

    ReplyDelete
    Replies
    1. its all duable, I have never yet played with bluetooth and the iphone but the idea doesnt seem to difficult. Did you ever code for ios or android?

      Delete
    2. Actually forget bluetooth for a minute. The next project I was about to start is using the ehernet shield. This one will be using a server and since the code behind the server will determine most of the "stuff" this could be easily implemented for ios and android.
      Also, since the shield can act as a server this is even more of a win win situation. I still havent decided if I will be implementing the server into the shield or running php code off of my website. In the future I will most likely end up doing both.

      Delete
    3. Hey Jim, Thanks for your post! where you able to PWM A LED and control with a slider thru an app? if yes can you please post the code it would be helpful or send to ekpamaku@gmail.com regards

      Delete
  3. everything seems to upload fine but im not getting a response from the led pin any suggestions? ive tried switching the rx and tx just to make sure i didnt have them backwards.

    ReplyDelete
  4. is attiny85 have minimum system ?
    It is look there is no minimum system from your circuit...

    ReplyDelete
  5. I am getting 0 as response every time on Attiny85, any idea why?

    ReplyDelete
  6. thank you i am able to connect now but i used different code. I did not try to compare code difference.

    ReplyDelete
    Replies
    1. Hi GS, can you please post to code that worked for you because am getting same problem! thanks a lot in advance it would be helpful

      Delete
  7. did anybody tried controlling servo through bluetooth attiny85? I tried different library of servo but did not work any idea.

    ReplyDelete
    Replies
    1. hey, sorry for the delay in the answer. yes I did this before. can i demonstrate it? not really to be honest, i havent played with the tiny for a while now. I was playing a lot with the atmega328 and making my own boards lately. But.. as for the tiny, I used the standard libary, make sure that the pins that your using support PWM.

      Delete
  8. Thanks a lot for your posting. I tried this. Initially the LED turns on . From the setup method i guess. But no response afterwards from bluetooth commands. BT is paired correctly. Any ideas ?

    ReplyDelete
  9. i am not getting any reply from bluetooth device ... but this tutorial s good ... everything went good but at last it went somewhere wrong i am not sure about that ... any body help me what ll be the issue...!!

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. HI
    I tried this but whenever I try to connect my HC-05 module through Putty, I get garbage characters printed on the black screen instead of the message 'Bluetooth Connection Successful'. What can be the problem?

    ReplyDelete
  12. Works BUT! : HC-05 RX --> attiny85 leg7(pin2)
    HC-05 TX --> attiny85 leg6(pin1)
    I think that part of the guide is misleading.

    And as I concluded in the guide we want to use the SerialMonitor BUT as we don't use TTL to USB converter it is impossible so watch out!

    Anyways Great Tutorial, Tkanks!

    ReplyDelete
  13. FOR ME THE CODE IS NOT WORKING THE PIN 4 ALWAYS REMAINS HIGH , I AM USING AN MOBILE APP TO CONTROL (BLUETOOTH 4 RELAY CONTROL) I HAD MODIFIED THE CODE BASED ON THE APP AS "A" TO ON ,"a" TO OFF PLZ HELP ME THE VERY SAME CODE IS WORKING WELL WITH ARDUINO UNO BUT IN ATTINY85 ITS NOT WORKING








    ReplyDelete
  14. I have bluetooth le 4.0 how can I use it with attiny 85 to control an led i also want to control 4 leds please help me

    ReplyDelete