Bluetooth con Attiny85

Fuente: http://arduinofy.blogspot.com/2013/05/attiny-bluetooth.html


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.

No hay comentarios:

Publicar un comentario