Worklog #1: Expanding LinkIt ONE/Arduino analog inputs.

2 minute read

While working on the controller for my RGB light system, I quickly noticed the analog inputs on the LinkIt ONE wouldn’t be enough. It only has three analog inputs while I needed at least 5 (2 for the controlling a joystick – which is basically two pots in one – and 3 for the color-choosing pots (R, G, B).

In order to increase the number of analog inputs, I decided to buy a 4052 4-channel (de)multiplexer. It costs roughly 50 cents and can handle both inputs and outputs: my local store had the ST version (HCF4052BE) so I bought that one, but TI also makes the same chip.

You can take a look at the datasheet here.

HCF4052B pinout.

Operation is fairly simple: you connect your analog devices to pins 1, 2, 4, 5 (for channel Y) or 11, 12, 14, 15 (for channel X). The signal will then be output from pin 3 (for channel Y) or pin 13(for channel X), depending on the high/low states on pins 9 and 10 (B and A, respectively), which are connected to your Arduino/LinkIt.

Take a look at the following table to understand how the thing works:

HCF4052B truth table.

This basically means that, provided that the INH pin (6) is connected to GND, if B and A are both low (connected to GND too), common pin 3 (or 13) will be connected to pin 1 – 0x (or pin 12 – 0y). If A is high (connected to VDD) and B is low, pin 3 (and 13) will then be connected to pin 5 (and 14), and so on.

Don’t forget to connect pin 16 to 5V and pin 6, 7 and 8 to GND.

Here is some example code to get this thing working for both Arduino and LinkIt, using only 3 of the 4 inputs. Try to modify the code to make it work for all pins!

In this particular case, I used this code to get the input from 3 potentiometers on the same analog pin.

// Digital "control" pins are D4 (CTRLA = A, pin 10) and D5 (CTRLB = B, pin 9).
// Analog in/out on 4052 pins 1,2,4 - corresponding to 0y, 2y, 3y as per the datasheet.
// 4052 common output (pin 3) connected to Arduino/LinkIT analog input 0 (A0).

#define	APIN	A0
#define CTRLA 	8
#define CTRLB	9

int r, g, b;
char buff[50];

void setup() {
	pinMode(APIN, INPUT);
	pinMode(CTRLA, OUTPUT);
	pinMode(CTRLB, OUTPUT);
	Serial.begin(9600);
}

void getValues(int *r, int *g, int *b){
	digitalWrite(CTRLA, LOW);
	digitalWrite(CTRLB, LOW);
	*r =analogRead(APIN);
	digitalWrite(CTRLB, HIGH);
	*g = analogRead(APIN);
	digitalWrite(CTRLA, HIGH);
	*b = analogRead(APIN);
}
void loop() {
	getValues(&r, &g, &b);
	sprintf(buff, "M,%d,%d,%d$", r,g,b);
	Serial.println(buff);
}

And that’s it!