Skip to content

Instantly share code, notes, and snippets.

@Lif3line
Last active February 6, 2024 12:57
Show Gist options
  • Save Lif3line/1c2029fb561d77958b15 to your computer and use it in GitHub Desktop.
Save Lif3line/1c2029fb561d77958b15 to your computer and use it in GitHub Desktop.
Arduino code to wake up upon interrupt from a bluetooth module and toggle a pin each time (here controlling a relay hence relayPin).
#include <avr/sleep.h>
#include <SoftwareSerial.h>
SoftwareSerial blueSerial(3, 9); // RX, TX pins
int relayActive = false;
int relayPin = 7;
void setup() {
blueSerial.begin(9600); // Set baud rate
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
attachInterrupt(1, toggleRelay, RISING); // Interupt for wake up
blueSerial.print("Initialised\n\r");
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Lowest power mode
sleep_enable();
sleep_mode(); // Go to sleep
}
void loop() {
sleep_mode();
}
void toggleRelay() {
blueSerial.print("Woah much intteruption!\n\r");
if (!relayActive) {
digitalWrite(relayPin, LOW);
} else {
digitalWrite(relayPin, HIGH);
}
relayActive = !relayActive;
}
@hakan1581
Copy link

It's clear now thank you. Is it appropriate to use the hc06 bluetooth module according to this code? Also do I need to use tx-rx pins or digital 1 / 2 (interrupt) pins?

@Lif3line
Copy link
Author

Lif3line commented Feb 6, 2024

No worries, I had this running with either a HC05 or HC06 module, so that should be fine. I think you'll need tx-rx pins for the serial then the mapping to a digital pin to attach the interrupt to. If software serial already does that then you should be set.

@hakan1581
Copy link

hakan1581 commented Feb 6, 2024

So should this command be used:

"attachInterrupt(digitalPinToInterrupt(RX_PIN)"

instead of ;

"attachInterrupt(1,

also even though the bluetooth connection is established, ("Initialised\n\r") message is not coming to the application screen I use on the phone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment