Thoughts, news and information about the world as seen through RV windows
RSS icon Home icon
  • Arduino Bar Graph

    Posted on February 16th, 2021 admin No comments

    As part of my fooling around with the PWM solar charge controller developed by young Indian electronics engineer and hobbyist Debasish Dutta, I decided to include a LED bar graph to show both photovoltaic and load current. One graph will do both, since the PV works only during the day and the load (a series of LED path lights) will run only at night.

    So I happily started writing code, and was quite pleased when I got it to work. Then I happened to be looking at the Arduino examples, and noticed (File > Examples > Display > Bar Graph). What, did I re-invent the wheel, so to speak?

    Yes, I did, even to the point of using an array to define the LEDs and assign them as outputs:

    segment[10] = {44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
    }
    for (int i = 0; i <= 9; i++) {
    pinMode (segment[i], OUTPUT);
    }

    Ditto for using the map() function to reassign the range, then using a for loop to turn the LEDs on (and off, code not shown)

    int solar_val = map(solar_current, 0, max_solar_current, 0, 9);
    for (int i = 0; i <= solar_val; i++) {
    digitalWrite(segment[i], HIGH);
    }

    Here’s the corresponding code from the Arduino tutorial (many parts omitted; this code will not work!)

    int ledPins[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
    // an array of pin numbers to which LEDs are attached
    void setup() {
    // loop over the pin array and set them all to output:
    for (int thisLed = 0; thisLed < ledCount; thisLed++) pinMode(ledPins[thisLed], OUTPUT);
    // map the result to a range from 0 to the number of LEDs:
    int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
    //turn on individual leds
    for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // if the array element's index is less than ledLevel,
    // turn the pin for this element on:
    if (thisLed < ledLevel) {
    digitalWrite(ledPins[thisLed], HIGH);
    }

    I’m no expert at writing code for Arduino, so I was relatively pleased that I worked this out for myself, even if it was “re-inventing the wheel”.

    Leave a reply

Supporting your fulltime RV adventures and aspirations