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

hi, which command do i need to send through serial terminal to wake up?

@Lif3line
Copy link
Author

Lif3line commented Feb 5, 2024

This is really old, but should trigger off any rising edge on the serial interrupt. I think a modernised version would use attachInterrupt(digitalPinToInterrupt(RX_PIN), toggleRelay, RISING); to make sure we're attached to the correct interrupt.

@hakan1581
Copy link

This is really old, but should trigger off any rising edge on the serial interrupt. I think a modernised version would use attachInterrupt(digitalPinToInterrupt(RX_PIN), toggleRelay, RISING); to make sure we're attached to the correct interrupt.

Unfortunately, I still don't understand. Which command should we send after connecting with the Bluetooth module? Also, rx and tx pins are not used as interrupts, as far as I know, in order to use them as interrupts, they must be defined as digital pins using software serial, as you did in the code.

@Lif3line
Copy link
Author

Lif3line commented Feb 6, 2024

Ah past me was too clever for current me clearly. Well assuming the attached interrupt is correct then it should trigger off any serial message, so no specific command. Connect to the Bluetooth with something that supports a serial terminal then any data sent across the serial will trigger the relay toggle.

@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