After googling "arduino 4051" and finding little I decided to try to make a "tutorial" to help get folks going faster. Let me know if you see any problems with the fritzing file, my code or it's comments, really anything.
Running out of analog inputs? Try using a 4051 Multiplexer.
This project will show you how to connect an arduino (UNO R3) to a 4051, read all the pins, then print the data on the serial bus.
In my fritzing file I tried to color code the wires the best I could.
Red=Power
Black=Ground
Brown= 4051 Inputs
Blue=Digital
Yellow=Analog
Orange=AREF
I also wired/coded a TMP36 to input 0 of the 4051 to show one input being used for reference on where to put your code.
#define aref_voltage 3.3
void setup() { //4051 digital control pins
pinMode(8, OUTPUT); // s0
pinMode(9, OUTPUT); // s1
pinMode(10, OUTPUT); // s2
Serial.begin(9600);
analogReference(EXTERNAL);
}
void loop()
{ //Read Value of 4051 analog-in 0 by setting the values of s0,s1 and s2
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
delay(10); //not sure if this delay is strictly necessary
int readInZero = analogRead(0); // read the input pin
float voltage = readInZero * aref_voltage;
voltage /= 1024.0;
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
// now convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF);
Serial.println(" degrees F");
delay(10);
//repeat to read other pins (in this case analog in 1)
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
delay(10);
int readInOne = analogRead(0);
Serial.print("Sensor 2=");
Serial.println(readInOne);
//(analog in 2)
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
delay(10);
int readInTwo = analogRead(0);
Serial.print("Sensor 3=");
Serial.println(readInTwo);
//(analog in 3)
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
delay(10);
int readInThree = analogRead(0);
Serial.print("Sensor 4=");
Serial.println(readInThree);
//(analog in 4)
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(10);
int readInFour = analogRead(0);
Serial.print("Sensor 5=");
Serial.println(readInFour);
//(analog in 5)
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(10);
int readInFive = analogRead(0);
Serial.print("Sensor 6=");
Serial.println(readInFive);
//(analog in 6)
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
delay(10);
int readInSix = analogRead(0);
Serial.print("Sensor 7=");
Serial.println(readInSix);
//(analog in 7)
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
delay(10);
int readInSeven = analogRead(0);
Serial.print("Sensor 8=");
Serial.println(readInSeven);
delay(3000); //wait three seconds
}
Hopefully this code and fritzing file can help someone get their project up and running a little faster!
4051.fzz (10.6 KB)