Bluetooth connection - Arduino + ATtiny : Help needed

Hi guys,

I'm very new to Bluetooth with the arduino and need to send data from an analog sensing circuit to the arduino using 2 RN-42s. I have read lots of posts, blogs and videos and it's still not fully clear to me.

The setup:

1 arduino + 1 ATtiny microcontroller (couldn't afford a second arduino for this)
2 RN-42 XV
1 Analog circuit - LED and photodiode

The ATtiny will monitor the input, transmit it to the arduino
The arduino will receive the transmitted data and display it on the serial monitor.

Any help will be very much appreciated!

Below is the code i have so far:

}
int sense = 0; //sensing analog value (actually from 0 - 5V)
int pulse = 13; //pulse with input
int tx = 2; // transmit

SoftwareSerial bluetooth(tx); //just transmit input

void setup() 
{
 
  Serial.begin(115200);
  pinMode(sense,INPUT);
  pinMode(pulse,OUTPUT);
  bluetooth.println(analogRead(0)); //print the detected value
  delay (100);
  bluetooth.begin(115200);



void loop() {
   
  if (Serial.available() > 900) //expecting analog signal to vary between 0 - 1023
  {
    char data = Serial.read(); //read if data available, store in data
    Serial.println(data); // display sensed value
    
   
   }
}

Thanks!

bluetooth.println(analogRead(0)); //print the detected value
  delay (100);
  bluetooth.begin(115200);

println 'ing before you even initialized the bluetooth software port! wrong! swap these statements.

 if (Serial.available() > 900) //expecting analog signal to vary between 0 - 1023
  {
    char data = Serial.read(); //read if data available, store in data
    Serial.println(data); // display sensed value
    
   
   }

make the it(Serial.available() > 0){}

  pinMode(sense,INPUT);

essentially you are making the pin number 0 that is the RX input so how'll you now receive anything?

Hi NI$HANT, thanks for the reply.

I have made the recommended changes :

int sense = 1; //sensing analog value (actually from 0 - 5V)
int pulse = 13; //pulse with input
int tx = 2; // transmit
//int check = 0;//flag to check status of input
SoftwareSerial bluetooth(tx); //just transmit input

void setup() 
{
 
  Serial.begin(115200);
  pinMode(sense,INPUT);
  pinMode(pulse,OUTPUT); 
  delay (100);
  bluetooth.begin(115200);

}

void loop() 

{
     if (Serial.available() > 0) //expecting signal to vary between 0 - 1023
       {
         data = Serial.read(); //read if data available, store in data
         Serial.println(data); // display sensed value
   
        }
}

Am i on track? I just need to read the analog value in the receive pin and transmit it - to another RN42

Thanks

Hi all, I have made changes to the code:

#include <SoftwareSerial.h>
int sense = 0; // read from analog pin 0
int check = 0; //initialising check
SoftwareSerial mySerial(6, 5);
 
void setup() {
  Serial.begin(115200);
  Serial.println("Detected = ");
 
  mySerial.begin(115200);
  mySerial.println("Sense Value = ");
}
 
void loop() {
  
  check = analogRead(sense);
  Serial.println(check);
  
  if (Serial.available())
  {
     mySerial.write(Serial.read());
  }
  
  if (mySerial.available())
  {
    Serial.write(mySerial.read());
    Serial.println(check);
  }
  Serial.flush();
  delay(100);
}

I have no issue receiving the data and reading it on the serial monitor (from the analog input)

My issue is trying to transmit it - so it appears on the Bluetooth COM Port. At the moment it doesn't

Please, any help will be very much appreciated

Thanks

My issue is trying to transmit it - so it appears on the Bluetooth COM Port. At the moment it doesn't

You have the code for one uC and you have told that its for Arduino or the ATtiny?
so give the other set of code that you have for either of the Arduino or ATtiney!

secondly I'm still not getting what exactly you want? I mean you want to receive the info using the bluetooth on the arduino from the ATtiny and then on the Arduino COM port show what you got over bluetooth?

Hi, thanks

Yes, the code i'm currently writing is to be eventual loaded unto the ATtiny - hence why it is reading the analog input and transmitting.

The second code will be similar - but will be to receive - this will be kept on the arduino, then displayed n the serial monitor.

Yes, in essence i can then have a portable signal monitor

So whats there not letting you do what you want?

At the moment, i'm only communicating between the arduino (future ATtiny program) and the Bluetooth - to see if it transmits.

Am i missing something?

Thanks

I'm not able to see the values being sensed by the arduino on the the Bluetooth COM port

So i'm making the assumption it is not transmitting anything

I'm using putty

Thanks

so you mean you have the serial connections of the RN42 hooked to the USB port directly?
and connecting to it using somehting which has the bluetooth like the android ?

OR

you have connected the RN42 to the arduino and then the arduino is connected to the computer and using the com port as such created to read the input?

At the moment:

The analog light sensing circuit is attached to the arduino's analog input A0 - to read variations in light levels.

The arduino is connect via USB to the computer - to view the serial monitor

The RN-42 is connected to the Arduino only -with RX and TX on digital pins 5 and 6.

Is there something that shouldn't be there?

Thanks

OK connection ideology is fine! (dont know whether you connected all that right?)

so how you expect the RN42 to get anything? I mean you need to send to it or read from it only when the bluetooth is paired to some phone and a serial link has been successfully establish and you use an app like the bluetooth chat available for android, if you have not paried any thing to the bluetooth hence not sending anything to it.

Oh the Bluetooth only sends when paired? Not continuous transmission like radio - till you tune in to it?

That seems logical / obvious

But you get what i'm trying to do?

Oh the Bluetooth only sends when paired? Not continuous transmission like radio - till you tune in to it?

You need to pair it PERIOD
(whether its bluetooth supporting embedded system to another bluetooth supporting embeded system or a bluetooh supported computer or phone)

then based on the profiles supported by the bluetooth module its stack works! in your case its the SPP profile.

Thanks

So code-wise, is what i have correct? Can i now just upload it to the ATtiny, modify the code for the arduino and pair them?

Codewise I already told you to make ammendments so just try now ,just worry about the logic levels that is RN42 is 3.3v and the atmega should be 3.3v level too! else use a logic level converter. also connect everything correctly.

I was hoping i could simply upload my arduino code to the ATtiny - NOT the case.

It doesn't support Serial.begin or any other serial functions.

Any ideas now how to get the data being sensed transmitted?

I was hoping i could simply upload my arduino code to the ATtiny - NOT the case.

I have already tested and coded within the Arduino IDE 0022/1.0 for ATtiny85 and used UART too that to successfully with the 9600baud rate to and fro info. from a bluetooth module, so it works all fine.
and since then did a lot many times!

You have to use software serial library for this and also ,go search for MIT labs attiny85 on google if you havent get going with their suggestions at first.

Hi, thanks for the reply. I'm understanding this a bit better now.

I decided to achieve basics first - turning an LED on and off through bluetooth with my laptop and the ATtiny.

heres a sample code i have modified:

#include <SoftwareSerial.h>

int rx = 1; //recieve to 1
int tx = 2; // transmit to pin 2

SoftwareSerial blueToothSerial(rx,tx);

int led = 0;

void setup()
{
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  setupBlueToothConnection();
  
  pinMode(led,OUTPUT);
  digitalWrite(led,HIGH); //initialising LED state
}

void loop()
{
  char recvChar; //check if data sent from bluetooth
  if (blueToothSerial.available())//if data available, read!
  {
    recvChar = blueToothSerial.read(); //received character
    
    if(recvChar == 'a')
    {
       digitalWrite(led,LOW);
    }  
    else if(recvChar =='b')
    {
       digitalWrite(led,HIGH);
       //delay(1000);
       digitalWrite(led,LOW);
       //delay(1000);
       digitalWrite(led,HIGH);
    }
       
    else
     {
       digitalWrite(led,HIGH);
     }
     
  }
  blueToothSerial.flush();
}

void setupBlueToothConnection()
{
  blueToothSerial.begin(115200);//default baud rate
  blueToothSerial.print("\r\n+STWMOD=0\r\n");//slave mode BT
  blueToothSerial.print("\r\n+STNA=RN42\r\n");//set name
  blueToothSerial.print("\r\n+STOAUT=1\r\n");//permit connection
  blueToothSerial.print("\r\n+STAUTO=0\r\n");//Auto-connection
  
  //delay(2000);//required delay
  //blueToothSerial.print("\r\n+INQ=1\r\n"); //BT is inquirable
  blueToothSerial.println("Praise God, Connected!\n");
  
  //delay(2000);//required delay
  
}

The problem with this is that the LED (connected to the ATtiny) always turns off regardless of what character is pressed - and stays off. I am sending characters through the serial monitor on the arduino IDE

What is wrong?

Thanks

int rx = 1; //recieve to 1
int tx = 2; // transmit to pin 2

SoftwareSerial blueToothSerial(rx,tx);

int led = 0;

void setup()
{
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);

Well that is wrong! you just don't need to set the pinMode for the uart pins just use the pins you want to implement UART on in the constructer and thats all!, who told you to do pinMode on software serial pins?

Sample code for ATtiny85 that works:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4);

void setup()  
{
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  pinMode(0, OUTPUT);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
   if(mySerial.available() > 0) {
     char input = mySerial.read();
     if (input == 'n') {
       mySerial.println("N was there"); 
       digitalWrite(0, 1);
     }
     if (input == 'a') {
       mySerial.println("A was there"); 
       digitalWrite(0, 0);
     }
   }
}
  blueToothSerial.begin(115200);//default baud rate

also try at a comparatively low rate like 9600