Skip to content

Commit 090fe65

Browse files
authored
Added setGain for LNA Gain (#408)
* Added setGain for LNA Gain Added setGain for LNA Gain * Update API.md Added API for setGain * Added example to use setGain Just use setGain after LoRa.begin // set maximum LNA for better RX sensitivity // 0: ADC is used and LNA gain is not used. // 1-6: ADC is not used and LNA gain is used. LoRa.setGain(6); * Fixed spacing only To change tabs to spaces. * Delete LoRaReceiverCallbackWithLNAGain.ino To remove as unnecessary. * To add example for setGain To add example for setGain as an optional setting. * Added setGain To add setGain * Fixed typo for AGC Fixed typo for AGC * Fixed comment for LoRa.setGain(6) * Make comment for setGain simpler Make comment for setGain simpler
1 parent 21ec3d4 commit 090fe65

File tree

5 files changed

+85
-43
lines changed

5 files changed

+85
-43
lines changed

API.md

+11
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ LoRa.enableInvertIQ();
363363
364364
LoRa.disableInvertIQ();
365365
```
366+
### LNA Gain
367+
368+
Set LNA Gain for better RX sensitivity, by default AGC (Automatic Gain Control) is used and LNA gain is not used.
369+
370+
```arduino
371+
LoRa.setGain(gain);
372+
```
373+
374+
* `gain` - LNA gain
375+
376+
Supported values are between `0` and `6`. If gain is 0, AGC will be enabled and LNA gain will not be used. Else if gain is from 1 to 6, AGC will be disabled and LNA gain will be used.
366377

367378
## Other functions
368379

Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1-
#include <SPI.h>
2-
#include <LoRa.h>
3-
4-
#ifdef ARDUINO_SAMD_MKRWAN1300
5-
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
6-
#endif
7-
8-
void setup() {
9-
Serial.begin(9600);
10-
while (!Serial);
11-
12-
Serial.println("LoRa Receiver Callback");
13-
14-
if (!LoRa.begin(915E6)) {
15-
Serial.println("Starting LoRa failed!");
16-
while (1);
17-
}
18-
19-
// register the receive callback
20-
LoRa.onReceive(onReceive);
21-
22-
// put the radio into receive mode
23-
LoRa.receive();
24-
}
25-
26-
void loop() {
27-
// do nothing
28-
}
29-
30-
void onReceive(int packetSize) {
31-
// received a packet
32-
Serial.print("Received packet '");
33-
34-
// read packet
35-
for (int i = 0; i < packetSize; i++) {
36-
Serial.print((char)LoRa.read());
37-
}
38-
39-
// print RSSI of packet
40-
Serial.print("' with RSSI ");
41-
Serial.println(LoRa.packetRssi());
42-
}
43-
1+
#include <SPI.h>
2+
#include <LoRa.h>
3+
4+
#ifdef ARDUINO_SAMD_MKRWAN1300
5+
#error "This example is not compatible with the Arduino MKR WAN 1300 board!"
6+
#endif
7+
8+
void setup() {
9+
Serial.begin(9600);
10+
while (!Serial);
11+
12+
Serial.println("LoRa Receiver Callback");
13+
14+
if (!LoRa.begin(915E6)) {
15+
Serial.println("Starting LoRa failed!");
16+
while (1);
17+
}
18+
19+
// Uncomment the next line to disable the default AGC and set LNA gain, values between 1 - 6 are supported
20+
// LoRa.setGain(6);
21+
22+
// register the receive callback
23+
LoRa.onReceive(onReceive);
24+
25+
// put the radio into receive mode
26+
LoRa.receive();
27+
}
28+
29+
void loop() {
30+
// do nothing
31+
}
32+
33+
void onReceive(int packetSize) {
34+
// received a packet
35+
Serial.print("Received packet '");
36+
37+
// read packet
38+
for (int i = 0; i < packetSize; i++) {
39+
Serial.print((char)LoRa.read());
40+
}
41+
42+
// print RSSI of packet
43+
Serial.print("' with RSSI ");
44+
Serial.println(LoRa.packetRssi());
45+
}

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enableCrc KEYWORD2
4747
disableCrc KEYWORD2
4848
enableInvertIQ KEYWORD2
4949
disableInvertIQ KEYWORD2
50+
setGain KEYWORD2
5051

5152
random KEYWORD2
5253
setPins KEYWORD2

src/LoRa.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,32 @@ void LoRaClass::setOCP(uint8_t mA)
607607
writeRegister(REG_OCP, 0x20 | (0x1F & ocpTrim));
608608
}
609609

610+
void LoRaClass::setGain(uint8_t gain)
611+
{
612+
// check allowed range
613+
if (gain > 6) {
614+
gain = 6;
615+
}
616+
617+
// set to standby
618+
idle();
619+
620+
// set gain
621+
if (gain == 0) {
622+
// if gain = 0, enable AGC
623+
writeRegister(REG_MODEM_CONFIG_3, 0x04);
624+
} else {
625+
// disable AGC
626+
writeRegister(REG_MODEM_CONFIG_3, 0x00);
627+
628+
// clear Gain and set LNA boost
629+
writeRegister(REG_LNA, 0x03);
630+
631+
// set gain
632+
writeRegister(REG_LNA, readRegister(REG_LNA) | (gain << 5));
633+
}
634+
}
635+
610636
byte LoRaClass::random()
611637
{
612638
return readRegister(REG_RSSI_WIDEBAND);

src/LoRa.h

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class LoRaClass : public Stream {
7777
void disableInvertIQ();
7878

7979
void setOCP(uint8_t mA); // Over Current Protection control
80+
81+
void setGain(uint8_t gain); // Set LNA gain
8082

8183
// deprecated
8284
void crc() { enableCrc(); }

0 commit comments

Comments
 (0)