charliplex 6 led * 3 pins

can anybody help increase this to a 5 pin or more and how to alter the wiring to match I have been at this for hours and now am struggling
(wood and the trees syndrome)

define LED_1 5
#define LED_2 6
#define LED_3 7

void setup()
{
  pinMode(LED_1, INPUT);
  pinMode(LED_2, INPUT);
  pinMode(LED_3, INPUT);
}

void loop()
{
  set_pins(LED_1, LED_2);
  delay(100);
  set_pins(LED_2, LED_1);
  delay(100);
  set_pins(LED_3, LED_1);
  delay(100);
  set_pins(LED_1, LED_3);
  delay(100);
  set_pins(LED_2, LED_3);
  delay(100);
  set_pins(LED_3, LED_2);
  delay(100);
     set_pins(LED_2, LED_3);
  delay(100);
  set_pins(LED_1, LED_3);
  delay(100);
  set_pins(LED_3, LED_1);
  delay(100);
  set_pins(LED_2, LED_1);
  delay(100);
}

void set_pins(int high_pin, int low_pin)
{
  reset_pins();
  pinMode(high_pin, OUTPUT);
    pinMode(low_pin, OUTPUT);
    digitalWrite(high_pin, HIGH);
    digitalWrite(low_pin, LOW);
 }

void reset_pins()
{
  pinMode(LED_1, INPUT);
  pinMode(LED_2, INPUT);
  pinMode(LED_3, INPUT);
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_3, LOW);
}
[code]

charli.jpg

After 17 posts I would expect you to know about code tags... So please edit your post and add code tags.

And just this morning I wrote this post. With the excel sheet (and if you know how Charlieplexing works) it should not be to hard to edit the code for 5 pin aka 20 leds.

my apologies
I might have made 17 posts but don't have clue what you mean by code tags

Then it's about time to read the sticky on top of every board called "How to use this forum" :wink:

Hello

Once you have done that, i can tell you about a different way to think about charlieplexing, which makes drawing the schematics and writing the code much easier (and shorter), for any number of pins.

Paul

195dcc3cd606a3360a277f04541ecbb1f621a009.jpg

I will take this as a learning experience and read the mentioned stickys,
and once my apologies

OK, thanks for doing that.

So, this is the way I think about charlieplexing. To me its much easier.

Lets take your 3 pin example and draw a different diagram.

Imagine wires extending out in parallel from the 3 Arduino pins, via the 3 series resistors. Put the 6 leds in a line above them. Because there are 3 pins, group the leds into 3 groups. Each group therefore has 2 leds.
c1.png

ok I am with you so far

Next, common-up the anodes in each group. Attach each common anode to one of the 3 wires.
c2.png

Then wire up the cathodes. The cathodes in each group should connect to the lines not already used to connect the anode of that group. As you go left to right in each group, use the wires top to bottom, in a methodical pattern.
c3.png

Now some code. Start by putting the pin numbers in an array like this:

#define PINS 3

const byte pin[PINS] = {5,6,7};

Lets make the sketch light each led in turn with a for() loop:

  for (byte i = 0; i < PINS * (PINS - 1); i++) {

  }

still following you am going to get pen and paper and try with more leds and lines to see if I understand will post on here so you can have a look and tell me if I am getting there or am way of

OK.

So i will go from 0 to 5 as we light each led in turn.

For each led i, we need to calculate the group that led is in and which member of that group it is:

    byte group = i / (PINS - 1);
    byte member = i % (PINS - 1);

However, we don't want "group" and "member" to be the same value, so if they are, we increment member:

    if (member >= group) {
      member++;
    }

Now we have identified the pin we want to make HIGH (=group) and the pin we want to make LOW (=member):

    pinMode(pin[group], OUTPUT);
    digitalWrite(pin[group], HIGH);

    pinMode(pin[member], OUTPUT);
    digitalWrite(pin[member], LOW);

Once the led is lit, keep it on for a little while and then reset the pins to INPUT:

    delay(500);

    pinMode(pin[group], INPUT);
    pinMode(pin[member], INPUT);

Completed code. I have not tested this but have done the same a few times in the past, so hopefully I’ll get it right.

#define PINS 3

const byte pin[PINS] = {5, 6, 7};

void setup() {

}

void loop() {
  for (byte i = 0; i < PINS * (PINS - 1); i++) {

    byte group = i / (PINS - 1);
    byte member = i % (PINS - 1);

    if (member >= group) {
      member++;
    }

    pinMode(pin[group], OUTPUT);
    digitalWrite(pin[group], HIGH);

    pinMode(pin[member], OUTPUT);
    digitalWrite(pin[member], LOW);

    delay(500);

    pinMode(pin[group], INPUT);
    pinMode(pin[member], INPUT);

  }
}

Diagram for 4 pins. Look away now if you don’t want to know the result.
c4.png

And for 5 pins:
c5.png

Mm, cool work Paul. I tried some sort of algorithm myself but couldn’t find one. But now I look at it i see a flaw… This is the truth table I get from you’re algorithm:

	0	1	2
0	p	n	
1	p	n	
2	n	p	
3		p	n
4	n		p
5		n	p

i = 0 and i = 1 will do the same…

But the code really helped me making the real algorithm! (aka, spot the error)

if (member == group) {
//should be
if (member >= group) {

My total code: (don’t like to define a number that the compiler already knows…)

//Pins that drive the charlieplexing
const byte Pins[] = {5, 6, 7};

//turns on a specific led of the charlieplex
void turnOnLed(byte nr){
  //do nothing if the number is to high
  if(nr >= numberOfLeds() ){
    return;
  }
  
  //calculate the group (positive end) and member (negative end)
  byte group = nr / (sizeof(Pins) - 1);
  byte member = nr % (sizeof(Pins) - 1);

  //Here is the fix :D
  //correct for itself not counting
  if (member >= group) {
    member++;
  }
  
  //Set each pin corresponding
  for(byte i = 0; i < sizeof(Pins); i++){
    if(i == group){
      pinMode(Pins[i], OUTPUT);
      digitalWrite(Pins[i], HIGH);
    }
    else if(i == member){
      pinMode(Pins[i], OUTPUT);
      digitalWrite(Pins[i], LOW);
    }
    else{
      pinMode(Pins[i], INPUT);
    }
  }
}

//returns the number of leds based on the number of pins
inline byte numberOfLeds(){
  return sizeof(Pins) * (sizeof(Pins) - 1);
}

To test the algorithm I make a spreadsheet. That’s what made the truth table. I added this to the first lookup table spreadsheet I made. Here is the Google Sheet.

PS. “coding” in Excel isn’t fun :stuck_out_tongue:

Except that the resistors are in the wrong place.

Why? If the LED are all the same (aka, need the same resistor) you can just do it like that and use resistors that are halve of what you need. At all times you have two resistors in series with the led :slight_smile:

thanks Paul you made it easier for me,
5 pins equals 4 leds so if I want 10 leds I need 11 pins looks like a arduino mega is needed
but once again thanks for your help

Huh, 5 pins equals 20 charlieplexed leds...

I meant 10 leds per group