Attiny85 microcontroller. Bluetooth HC-06. Arduino IDE

Hello friends,

these days I have been playing with the Attiny85 board and I would like to share my experience in this Community.

I have done this tutorial in Spanish, in case you want to take a look at it:
http://kio4.com/arduino/67_Attiny85.htm

ooooooooooooooooooo0000oooooooooooooooooooo

This chip is TINY85 microcontroller.
tiny85_50

  • It has a built-in clock and USB driver (therefore it can work stand-alone)
  • PWM, ADC, INTerruptions, BUS I2C,...
  • Can be programmed with the Arduino IDE
  • But only 6 kB of memory for our Sketchs :frowning_face:

ooooooooooooooooooo0000ooooooooooooooooooooooooooooooo

We can also find it inserted in an ATTINY85 board.

  • The model on the left is inserted directly into the USB connector. Beware, hackers are using this model to create fake USB sticks, as it can simulate a keyboard.

  • The model on the right uses a USB-microUSB cable to connect to the PC. This is the model that I will use by itself the most comfortable.

ooooooooooooooooooo0000ooooooooooooooooooooooooooooooo

- Installation Instructions. Arduino IDE:

http://digistump.com/wiki/digispark/tutorials/connecting

ooooooooooooooooooo0000ooooooooooooooooooooooooooooooo

Upload code.

  • To upload the code (Sketch), you must disconnect the card from the PC. When you get this prompt, please connect the board.

  • Plug board now...

tiny85_24

2 Likes

1.- App turns ON/OFF a LED4 in ATTINY85 by Bluetooth HC-06. Tiny85 returns a response. (E/A)

// Tiny85 y Bluetooth HC-06
// Juan A. Villalpando
// kio4.com

#include <SoftSerial.h>
#include <TinyPinChange.h>
#define LED4 4

SoftSerial Bluetooth(2,3);
char caracter;

void setup() {
  Bluetooth.begin(9600);
  pinMode(LED4, OUTPUT);}
  
void loop()
{
  while(Bluetooth.available() > 0)
  {
    caracter = Bluetooth.read();
    if (caracter == '1'){ 
      digitalWrite(LED4, HIGH);
      Bluetooth.println('E');}
      
    if (caracter == '0'){ 
      digitalWrite(LED4, LOW);
      Bluetooth.println('A');}
  }
}

p9_67Tiny85BTi.aia (2.8 KB)

2 Likes

2.- TINY85 stand-alone. Tinkercad.

a) We can program the TINY85 chip with this card ...

tiny85_10

b) We can also program the TINY85 chip with Arduino UNO.

ooooooooooooooooooooooo0000oooooooooooooooooooooo

  • Once the TINY85 chip is programmed, it can work stand-alone, since the clock and the USB driver have it integrated.

tiny85_19B

ooooooooooooooooooooooo0000oooooooooooooooooooooo

  • Tinkercad is a website where we can simulate circuits, resistors, LEDs, capacitors, Arduino, Tiny85 ...

Examples with tinkercad and Attiny85:
https://mjvo.github.io/tutorials/circuits/attiny85/

1 Like

3.- ATTINY85. PushButton. Bluetooth HC-06.

// Juan A. Villalpando
// kio4.com
// Pulsador. Bluetooth.
		
#include <SoftSerial.h>
#define Pulsador0 0
#define LED1 1
int valor;

SoftSerial Bluetooth(2,3);
char caracter;

void setup() {
  Bluetooth.begin(9600);
  pinMode(Pulsador0, INPUT);
  pinMode(LED1, OUTPUT);
}

void loop() {
  valor = digitalRead(Pulsador0);
  if (valor == HIGH) { 
      digitalWrite(LED1, HIGH);
      Bluetooth.println("PushButton ON");
  } 

  if (valor == LOW) { 
      digitalWrite(LED1, LOW);
      Bluetooth.println("PushButton OFF");
  } 
delay(200);  
}

p9_67Tiny85BT_Pulsai.aia (2.8 KB)

1 Like

4.- ATTINY85. Potentiometer. Bluetooth HC-06.

Pin P4 is called A2 in the code when used as an ADC.

// Juan A. Villalpando
// kio4.com
// Potenciómetro. Bluetooth.

#include <SoftSerial.h>
int pot_valor = 0;
String pot_valorS = ""; // pot_valor en String.
#define LED1 1 

SoftSerial Bluetooth(2,3);

void setup(){ 
Bluetooth.begin(9600);
pinMode (LED1,OUTPUT); 
pinMode (A2,INPUT); // Entrada ADC del terminal 4
} 

void loop(){ 
    pot_valor = analogRead(A2);
    // pot_valor = map(pot_valor, 0, 1023, 0, 255);
  pot_valorS = String(pot_valor);
  Bluetooth.println(pot_valorS);
  if (pot_valor > 500)
  {digitalWrite(LED1,HIGH);}
  else
  {digitalWrite(LED1,LOW);}

  delay(200); 
} 
			

p9_67Tiny85BT_Potenciometroi.aia (3.5 KB)

1 Like

5.- App sends text to ATTINY85 and shows in a LCD-I2C.

// Juan A. Villalpando
// kio4.com
 
#include <TinyWireM.h> 
#include <LiquidCrystal_attiny.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SoftSerial.h>

SoftSerial Bluetooth(1,3);
char caracter;
String palabra;

void setup() {
  Bluetooth.begin(9600);
  lcd.init();
  lcd.backlight(); 
}
 
void loop() {

if(Bluetooth.available()){
    caracter = Bluetooth.read(); 
    palabra = palabra + caracter; 
  if (caracter == '*') {
    palabra = palabra.substring(0, palabra.length()-1); // Quita el *
    int commaIndex = palabra.indexOf(',');
    lcd.clear();
    lcd.setCursor(0, 0); 
    lcd.print(palabra.substring(0, commaIndex));
    lcd.setCursor(0, 1); 
    lcd.print(palabra.substring(commaIndex+1));
    palabra = "";     }
  }
  delay(100);
}
			

p9_67Tiny85BT_Textoi.aia (2.7 KB)

3 Likes

I need a faster response (BT latency is not acceptable). Can I connect the board through USB to the phone and have Android read the usb value? And what's the attiny85 curse to put the reading of the buttons on the usb bus?
In case the above want clear: I need Android app to react immediately to the pushing of an external physical button.

Arduino and Phone with cable OTG and extension:

Examples with HC-06 Bluetooth:

ESP32 has better features, but I haven't compared latencies.

1 Like

Dear @David_Schkolnik,
are you really sure that the BT is not fast enough ?
Please take a look at this:
image
Even with the worst speed (1Mbps) we are talking about 100 bytes every 1 millisecond (let's suppose 1 start bit + 8 data bits + 1 stop bit => each byte = 10 bits). Since you want to respond to a pushbutton, this means that you have also to consider the human responsiveness to a stimulus.
Normally I use a clock with a 10 milliseconds period in AI2 to poll the incoming BT line. If nothing has arrived, the clock exits immediately, otherwise if characters have arrived, i fetch all data 'till the end of buffer. (put -1 as termination and LF as end of transmission from BT),
On the Arduino board set the baudrate toward the HC06 @115200 (9600 is 10 times slower)
I use this procedure to get data incoming from CAN bus (@500 Kbits/sec) and I don't lose any data.
Give it a chance.
Kind regards.

PS Sorry Juan_Antonio it seems that I answered to you instead of @David_Schkolnik :upside_down_face:

2 Likes

Hi Ugo,

Thanks so much for your quick response. It's not the baud rate what hinders BT 's performance in this case but latency. Once data stats streaming through BT it can be fast, but the first package can take some milliseconds, and I don't have that time. But I'll give it a try with Arduino.

Thanks again.

Hi David, (@David_Schkolnik)
sorry, I misunderstood :upside_down_face:, since you were talking about the possible use of a USB instead of the BT, I thought it was a problem of the BT speed. Anyway you can still have two possible causes of "first frame" latency: the Arduino code or the AI2. As I told in my previous post, a 10 millisecond timer (clock) is suitable to get fast data on AI2 side, provided that, when it detectcs a not empty buffer, it fetches all data stored therein.
Best wishes !
Cheers, Ugo.

Hello David - need to see your Blocks. The fastest way the App can receive data via BLE is by using the Register for [value type] Block. A timer is not then required. Ensure you have the latest MIT BLE Extension:
https://www.professorcad.co.uk/appinventortips#TipsBluetooth

However, why on earth do you need to receive data so fast? Isn't it likely that you will end up receiving several rows of identical values?

Hi, thanks for your response.
I don't need to receive so much data. When a physical button is pressed, I need the device to read an analogy value and send it ASAP. What's important is the time from button pushed to Android receiving data.

1 Like

Dear @David_Schkolnik,
would it be possible for you to share your Arduino code please ?
It's becoming interesting why you have "latency" between the button push and the display on the phone.
Assumed that it's not a BT baudrate issue, nor a clock timing on AI2 (as I told previously I can get data as fast as 10ms), now it is the turn to take a look to the sender (the Arduino :wink: )
Do you apply some filtering routine when you acquire the pushbutton ? Are you waiting for some kind of handshake ?
Cheers, Ugo.