Miu Ling Lam

Media Art, Physcial Computing, Interaction Design – a blog connects me to like minds

Arduino Multiplexing Example 1

Processing code:

import processing.serial.*;
Serial myPort;

int numChannels = 16;

void setup() {
  size(1280,800);
//  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  background(0);
}

void draw() {
}

void mouseMoved() {
  int ID = int(map(mouseX,0,width,0,numChannels));
  myPort.write(ID);
}

Arduino code:

// 16 channels
int r0 = 0;
int r1 = 0;
int r2 = 0;
int r3 = 0;
int row = 0;
int ID = 0;
int bin [] = {0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};

void setup(){
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(115200);
}

void loop () {
  if (Serial.available()) {
    ID = Serial.read();
    Serial.print(ID);
  }
  row = bin[ID];
  r0 = row & 0x01;
  r1 = (row>>1) & 0x01;
  r2 = (row>>2) & 0x01;
  r3 = (ID>7)? 1:0;
  digitalWrite(2, r0);
  digitalWrite(3, r1);
  digitalWrite(4, r2);
  digitalWrite(5, r3);
}
Advertisements

Filed under: Physcial Computing, Programming

4 Responses

  1. Jorge says:

    hey miu!

    this is the first time i see a multiplexer in action; i read a bit about it and found out the 4051 can work as a multiplexer & demultiplexer to expand arduino. i’m just having some trouble understanding your circuit. i can see the 3 digital pins from arduino select the output of the second (from up-downwards) 4051, right? :S

    are you using the Z pins of the 4051s as inputs? is your circuit demultiplexing only?

    so many wires! 🙂

    this is what i think is happening, please tell me if i’m wrong (or right for that matter :D)

    the third 4051 gets 5V (signal) form arduino into its z pin and sends the voltage to the other two IC through 2 pins (yellow wire). then, three digital pins of the arduino select the outputs of the second 4051 that go to the LEDs. from here on i’m lost. i don’t know that the 4th digital pin is doing and how do the outputs of the first 4051 get selected.

    sorry for the long message, but i’m really excited about multiplexers and i’m eager to learn about their behavior. thanks in advanced!!

    😉

    ps. why is the baud rate for the serial communication different in both sketches?

    • miu says:

      Hey Jorge,

      There are 16 LEDs in this example but one 4051 can only do 8 channels. When you think about binary numbers – 3 pins will give you 2^3=8 (000, 001, 010, 011, 100, 101, 110 and 111) different combination. Adding one more pin will double the number of combination (so total 16).

      This is what the 4th pin does – when it is 1, it picks up the upper half of the LED array; when it is 0, it picks the lower half.

      And thanks for pointing out the baud rate mistake – i forgot to change it back when I tried higher baud rate.

      Many thanks and please show me your project.

      miu

  2. Ubuntu Guru says:

    Hey Miu, are your Led’s cathode ==> 4051 anode ==> +5V
    or anode ==> 4051 cathode ==> +5V ?

    Thanks Caesar

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: