Pranav Rokde
Published © CC BY-NC

ATtiny85 Bluetooth Automation With Button Feedback

Make Automation Simpler

IntermediateFull instructions provided1,014
ATtiny85 Bluetooth Automation With Button Feedback

Things used in this project

Hardware components

ATtiny85
Microchip ATtiny85
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Linear Regulator (7805)
Linear Regulator (7805)
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Resistor 1M ohm
Resistor 1M ohm
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×1
Capacitor 10 µF
Capacitor 10 µF
×1
Capacitor 100 µF
Capacitor 100 µF
×1
Capacitor 100 nF
Capacitor 100 nF
×2
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×2
LED (generic)
LED (generic)
×1
MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
TaydaElectronics MINI RELAY SPDT 5 PINS 12VDC 10A 120V CONTACT
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Buzzer
Buzzer
×1
DC Power Connector, Jack
DC Power Connector, Jack
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Mini Side Cutter, 120mm Length with 25mm Jaw Capacity
Mini Side Cutter, 120mm Length with 25mm Jaw Capacity

Story

Read more

Schematics

Simple ATtiny 85 Bluetooth Automation

Code

ATtiny85 Bluetooth Automation With Button Feedback

C/C++
/*

  Simple ATtiny 85 Bluetooth Automation

  by: Pranav Rokde

  Bluetoooth Module ( HC-05 ) Connection

  RxD_(3) | IC Pin [2]  ---->  Tx Pin of Bluetooth Module
  TxD_(4) | IC Pin [3]  ---->  Rx Pin of Bluetooth Module

*/

#include"SoftwareSerial.h"

                   // Pin Mapping
#define RxD    3   // IC Pin [2]  Defining Rx Pin  || Tx Pin of Bluetooth Module
#define TxD    4   // IC Pin [3]  Defining Tx Pin  || Rx Pin of Bluetooth Module
#define RELAY  0   // IC Pin [5]  Defining Relay Connected to Load
#define BUTTON 2   // IC Pin [7]  Defining Push Button
#define BUZZER 1   // IC Pin [6]  Defining Buzzer

SoftwareSerial BT(RxD, TxD); // Rx|Tx

int RLY_FLAG = 0; // Relay Flag

int BUZZ_FLAG = 0; // Buzzer Flag

int BUTTONSTATE;                       // Current State of Button
int LASTBUTTONSTATE = HIGH;           // Previous State of Button
unsigned long LASTDEBOUNCETIME = 0;  // Last Toggle
unsigned long DEBOUNCEDELAY = 50;   // Debounce Time


void setup()
{

  pinMode(RELAY, OUTPUT);        // Set RELAY Pin as Output

  pinMode(BUZZER, OUTPUT);       // Set BUZZER Pin as Output

  pinMode(BUTTON, INPUT_PULLUP); // Set BUTTON Pin as Internally Pulled Up (HIGH)

  BT.begin(9600);                // Serial Begin for Bluetooth Module

}

void loop()
{

  BluetoothControl();  // Bluetooth Control Function

  ButtonControl();     // Push Button Function

  RelayControl();      // Relay Control Function

  BuzzerControl();     // Buzzer Control Function

}


void ButtonControl()
{

  int READING = digitalRead(BUTTON);   // Read Button

  if (READING != LASTBUTTONSTATE)      
  {
    LASTDEBOUNCETIME = millis();       
  }

  if ((millis() - LASTDEBOUNCETIME) > DEBOUNCEDELAY) {

    if (READING != BUTTONSTATE)
    {
      BUTTONSTATE = READING;

      if (BUTTONSTATE == LOW)
      {
        if ( RLY_FLAG == 0)  // Reading Relay Flag Status
        {

          BT.println('5');  // Send Data  
          RLY_FLAG = 1;     // Toggling Relay Flag

        }
        else if ( RLY_FLAG == 1) // Reading Relay Flag Status
        {

          BT.println('4');       // Send Data  
          RLY_FLAG = 0;          // Toggling Relay Flag

        }
      }
      delay(100); //Small delay
    }

  }

  LASTBUTTONSTATE = READING;    // Save the Reading

}




void BluetoothControl()
{

  if (BT.available())    // Serial Available to Recieve Data
  {

    char C = BT.read();  // Serial Read

    if (C == '1')        // Read Recieved Data 
    {
      RLY_FLAG = 1;      // Turning ON Relay Flag
    }

    if (C == '2')        // Read Recieved Data 
    {
      RLY_FLAG = 0;      // Turning OFF Relay Flag
    }

    if (C == '3')        // Read Recieved Data 
    {
      BUZZ_FLAG = 1;     // Turning ON Buzzer Flag
    }

    C = "";              //  Clear Serial Read

  }

}


void RelayControl()
{

  if ( RLY_FLAG == 0)          // Reading Relay Flag Status
  {
    digitalWrite(RELAY, LOW);  // Turning OFF RELAY
  }

  if ( RLY_FLAG == 1)          // Reading Relay Flag Status
  {
    digitalWrite(RELAY, HIGH); // Turning ON RELAY
  }

}


void BuzzerControl()
{

  if ( BUZZ_FLAG == 1)          // Reading Buzzer Flag Status
  {

    digitalWrite(BUZZER, HIGH); // Turning ON BUZZER

    delay(500);                 // Little Delay   

    digitalWrite(BUZZER, LOW);  // Turning OFF BUZZER
 
    BUZZ_FLAG = 0;              // Turning OFF Buzzer Flag
 
  }

}

Credits

Pranav Rokde

Pranav Rokde

2 projects • 1 follower

Comments