I have 2 MKR WAN 1310. One is the receiver which controls an LED, the other one is the sender which controls the LED of the receiver. The rate at which commands are sent is totally random, so the receiving Arduino must constantly be on. Therefore I want to reduce the power consumption of the receiver Arduino. The MKR currently uses around 32 mA when connected to an LED and 220 Ohm resistor. Here is my current code. I tried various power reduction commands but most of them like LowPower.Sleep() or LowPower.deepSleep() disables the LoRa communication.
#include <SPI.h>
#include <LoRa.h>
#include "ArduinoLowPower.h"
String contents = "";
bool ledStatus = false; // Track LED state
int led = 2; // LED pin
// Timer variables for LED auto-off after 6 hours
unsigned long startMillis = 0;
unsigned long ledOnDuration = 6 * 60 * 60 * 1000; // 60 hours in milliseconds
void setup() {
pinMode(led, OUTPUT); // Initialize the LED pin
// Initialize LoRa communication
if (!LoRa.begin(868E6)) {
// Flash LED to indicate failure
while (1) {
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
}
// Start with LED off
digitalWrite(led, LOW);
Serial.end();
USBDevice.detach();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
for (int i = 0; i < 15; i++) {
if (i != led) { // Skip pin 2 (the LED pin)
pinMode(i, INPUT); // Set unused pins to INPUT to save power
}
}
}
void loop() {
// Check for incoming LoRa packets
int packetSize = LoRa.parsePacket();
if (packetSize) {
// Read the packet content
contents = "";
while (LoRa.available()) {
contents += (char)LoRa.read();
}
// If the packet matches "button pressed"
if (contents.equals(buttonPress)) {
// Turn on the LED when the message is received
ledStatus = !ledStatus;
startMillis = millis(); // Start the timer for the 6 hours duration
}
if (ledStatus){
digitalWrite(led, HIGH);
}
else{
digitalWrite(led, LOW);
}
contents = "";
}
// Auto turn-off the LED after 6 hours
if (ledStatus && (millis() - startMillis >= ledOnDuration)) {
ledStatus = false;
digitalWrite(led, LOW); // Turn off LED
}
LowPower.idle(10);
}
Does anyone know how to reduce the power consumption for an MKR WAN 1310 receiver using LoRa in a P2P connection?