# Electron Signal Strength LED Algorithm

**URL:** <https://community.particle.io/t/electron-signal-strength-led-algorithm/28848>\
**Category:** Hardware\
**Created:** [January 25, 2017, 11:45am UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848 "2017-01-25T11:45:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbreedofgeek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/newbreedofgeek/32/31211_2.png) [@newbreedofgeek](https://community.particle.io/u/newbreedofgeek)\
**Post date:** [January 25, 2017, 11:45am UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/1 "2017-01-25T11:45:22Z")

</div>

Hello,

I’m building a IoT device that will work indoors and I use an Electron for connectivity.

The Electron has a handy feature, where if you press the ‘mode’ button once it’s breating blue it will flash the signal strength in a series of green lights

[https://docs.particle.io/guide/getting-started/start/electron/#display-signal-strength-](https://docs.particle.io/guide/getting-started/start/electron/#display-signal-strength-)  
“The signal strength (RSSI) will be shown in a 0-5 green blinks, 5 being the strongest”

I need to reproduce this logic using 5 LEDs on my device, so that a user can visually see the indoor signal strength as they move the device around and decide where best to place it (i.e. so they can place it with someplace with a strong signal)

Getting the RSSI via the firmware/user app API is easy:  
[https://docs.particle.io/reference/firmware/electron/#rssi-](https://docs.particle.io/reference/firmware/electron/#rssi-)

But can you please let me know what Algorithm/logic you use to translate the RSSI returned via the API into a 1-5 scale? so I can mirror the built-in, “mode” feature in my own 5 LED setup. I’d like to mirror this exactly so I’m confident in the logic.

Do you use the “qual” as well in your Algorithm?

Thanks very much,

Mark.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 25, 2017, 12:17pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/2 "2017-01-25T12:17:35Z")

</div>

The easiest would be to map the range via tha `map()` function.

Give this a shot

```cpp
  int strength = map(RSSI, -131, -51, 0, 5);

```

You may have to tweak the boundaries (-131/-51) a bit to get the mid point of each category right.

And this is the code from the firmware repo

```cpp
void system_display_rssi() {
    /* signal strength (u-Blox Sara U2 and G3 modules)
     * 0: < -105 dBm
     * 1: < -93 dBm
     * 2: < -81 dBm
     * 3: < -69 dBm
     * 4: < - 57 dBm
     * 5: >= -57 dBm
     */
    system_prepare_display_bars();
    int rssi = 0;
    int bars = 0;
#if Wiring_WiFi == 1
    rssi = WiFi.RSSI();
#elif Wiring_Cellular == 1
    CellularSignal sig = Cellular.RSSI();
    rssi = sig.rssi;
#endif
    if (rssi < 0) {
        if (rssi >= -57) bars = 5;
        else if (rssi > -68) bars = 4;
        else if (rssi > -80) bars = 3;
        else if (rssi > -92) bars = 2;
        else if (rssi > -104) bars = 1;
    }
    DEBUG("RSSI: %ddB BARS: %d\r\n", rssi, bars);

    system_display_bars(bars);
}

```

---

<div class="post-metadata">

**Author:** ![newbreedofgeek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/newbreedofgeek/32/31211_2.png) [@newbreedofgeek](https://community.particle.io/u/newbreedofgeek)\
**Post date:** [January 26, 2017, 5:42am UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/3 "2017-01-26T05:42:40Z")

</div>

Thank you very much @ScruffR  
That will work perfectly for me 😄

---

<div class="post-metadata">

**Author:** ![SolarChamp](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SolarChamp](https://community.particle.io/u/SolarChamp)\
**Post date:** [June 20, 2017, 3:09pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/5 "2017-06-20T15:09:23Z")

</div>

is there a way to do this with printing out the strength:

```auto
int rssi=0;
char str;
CellularSignal sig = Cellular.RSSI();
    rssi = sig.rssi;
void setup() {
 Time.zone(-4);
 Serial.begin(9600); 
}

void loop() {
    if (rssi<0)
{
    if (rssi>=-70) str="Excellent";
    else if(rssi>=-70 && rssi >-85) str=("Good");
    else if (rssi>-86&& rssi >=-100) str=("Fair");
    else if (rssi>-100) str=("Poor");
    else if (rssi>-110) str=("No Signal");
  
}
Serial.println(Time.format(Time.now(), "%Y-%m-%d %H:%M:%S"));
Serial.println(str);
}

```

something like this ? this however is not working

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 20, 2017, 3:16pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/6 "2017-06-20T15:16:45Z")

</div>

What exactly is not working?  
Apart from the obvious error of placing active code outside of any function 😉

---

<div class="post-metadata">

**Author:** ![SolarChamp](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SolarChamp](https://community.particle.io/u/SolarChamp)\
**Post date:** [June 20, 2017, 3:40pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/7 "2017-06-20T15:40:17Z")

</div>

The CellularSignal sig = Cellular.RSSI(); is giving me an error so not even able to get to the loop on printing out the strength, I was also wondering if this can be done in a case instead of all the if’s. Any help is welcome, Thank You

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 20, 2017, 6:26pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/8 "2017-06-20T18:26:44Z")

</div>

> [@SolarChamp](#):
>
> The CellularSignal sig = Cellular.RSSI(); is giving me an error

Yes, and what is the exact error message - don't withhold crucial info please

---

<div class="post-metadata">

**Author:** ![SolarChamp](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@SolarChamp](https://community.particle.io/u/SolarChamp)\
**Post date:** [June 20, 2017, 8:09pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/9 "2017-06-20T20:09:20Z")

</div>

Understand did mean to, I did get the code to verify after fixing bunch of things  
instead of trying to use char or arrays I just had it do a print out Serial.write for the different setting I wanted. Thank You tho for looking at it.

```cpp
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  system_display_rssi();
}

void system_display_rssi() {
 
    int rssi = 0;
 
#if Wiring_WiFi == 1
    rssi = WiFi.RSSI();
#elif Wiring_Cellular == 1
    CellularSignal sig = Cellular.RSSI();
    rssi = sig.rssi;
#endif
    if (rssi < 0) {
    if (rssi>=-70){Serial.write("Excellent"); }
    else if(rssi>=-70 && rssi >-85) {Serial.write("Good");}
    else if (rssi>-86&& rssi >=-100) {Serial.write("Fair");}
    else if (rssi>-100) {Serial.write("Poor");}
    else if (rssi>-110) {Serial.write("No Signal");}
    }
}

```

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [June 21, 2017, 12:52am UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/10 "2017-06-21T00:52:02Z")

</div>

> [@SolarChamp](#):
>
> if (rssi \< 0) {  
> if (rssi\>=-70){Serial.write("Excellent"); }  
> else if(rssi\>=-70 && rssi \>-85) {Serial.write("Good");}  
> else if (rssi\>-86&& rssi \>=-100) {Serial.write("Fair");}  
> else if (rssi\>-100) {Serial.write("Poor");}  
> else if (rssi\>-110) {Serial.write("No Signal");}  
> }

The logic here will not work as you expect; the first else-if statement will never be executed. If rssi \>= -70, the if statement will run, so the else-if statement will never be reached. The other conditions aren't correct either. For instance, if rssi == -71, you would want the result to be "Good", but given your logic, it would print "Fair". You don't need to "AND" any of the conditions together, you only need one condition for each statement. Something like this should do,

```cpp
if (rssi < 0) {
    if (rssi >= -70) {
        Serial.write("Excellent"); 
    }else if(rssi >= -85) {
        Serial.write("Good");
    }else if (rssi >= -100) {
        Serial.write("Poor");
    }else {
        Serial.write("No Signal");
    }
}

```

---

<div class="post-metadata">

**Author:** ![ovro67](https://avatars.discourse-cdn.com/v4/letter/o/49beb7/32.png) [@ovro67](https://community.particle.io/u/ovro67)\
**Post date:** [September 22, 2017, 4:53pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/11 "2017-09-22T16:53:54Z")

</div>

I can’t get my sketch to compile and I wondered if I need to add additional libraries for ‘system\_display\_bars’?

Would it be possible to display the Strength using an I2C 16x2 LCD Display using the lcd.print (char…) after defying the 5 chars? I’m not sure how this line displays the data, DEBUG(“RSSI: %ddB BARS: %d\r\n”, rssi, bars);.  
Thank you.

I’m getting the following errors:  
signalstrength.ino:69:29: error: ‘system\_display\_bars’ was not declared in this scope  
system\_display\_bars(bars);  
^  
signalstrength.ino:66:9: warning: unused variable ‘strength’ [-Wunused-variable]  
int strength = map(rssi, -131, -51, 0, 5); // was RSSI

```auto
// Manually added, not in Library
#include "Particle.h"

void setup() {
      
  Serial.begin(9600);
// int rssi = 0;
// int bars = 0;
}

void loop()
{
  
  system_display_rssi();
}
    /* signal strength (u-Blox Sara U2 and G3 modules)
     * 0: < -105 dBm
     * 1: < -93 dBm
     * 2: < -81 dBm
     * 3: < -69 dBm
     * 4: < - 57 dBm
     * 5: >= -57 dBm
     */

void system_display_rssi()
{
    system_prepare_display_bars();
    int rssi = 0;
    int bars = 0;

#if Wiring_WiFi == 1
    rssi = WiFi.RSSI();
#elif Wiring_Cellular == 1
    CellularSignal sig = Cellular.RSSI();
    rssi = sig.rssi;
#endif
    if (rssi < 0)
    {
        if (rssi >= -57) bars = 5;
        else if (rssi > -68) bars = 4;
        else if (rssi > -80) bars = 3;
        else if (rssi > -92) bars = 2;
        else if (rssi > -104) bars = 1;
    }
    int strength = map(rssi, -131, -51, 0, 5); // was RSSI
    DEBUG("RSSI: %ddB BARS: %d\r\n", rssi, bars);

    system_display_bars(bars);
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 22, 2017, 5:32pm UTC](https://community.particle.io/t/electron-signal-strength-led-algorithm/28848/12 "2017-09-22T17:32:31Z")

</div>

You need that function declared and implemented in your code.

The one in the code snippet I quoted above is only accessible to the _system_.
