Skip to content

Multiplexer Usage

Bennett Lewis edited this page Mar 15, 2020 · 12 revisions

Our project uses a CD74HC4051 multiplexer for iterating through the 8 temperature sensors on the rover. The pin out for the multiplexer can be found below.

Pin out for multiplexer

Pin Mapping

Digital pins 2, 3, 4, 5 on the Arduino should connect to channels 11, 10, 9, 6 on the multiplexer, respectively.

The 4 interior temperature sensors should be connected to channels A0-A3 on the multiplexer, with the 4 exterior sensors connected to channels A4-A7.

Analog read A0 on the Arduino should connect to channel A on the multiplexer.

Code Explanation

Using 3 digital write pins on the Arduino, we represent a 3-bit value (0-7) to be interpreted by the multiplexer. We define 3 variables to store the bytes:

byte s0;
byte s1;   // the three digital pins for the bits     
byte s2;

Next we initialize 3 variables for our digital write pins, and one enable pin:

int sel0  = 4; // Selecters 0,1,2 to pick channel
int sel1 = 3;
int sel2 = 2;

int enable = 5;

Within the setup function we initialize our byte variable to zeros and define our digital pins to be outputs:

 s0 = 0;
 s1 = 0;
 s2 = 0;
  
 pinMode(sel2, OUTPUT);    // s0
 pinMode(sel1, OUTPUT);    // s1
 pinMode(sel0, OUTPUT);    // s2
 pinMode(enable,OUTPUT);   //vE

Within the Arduino's main loop function we first write to our enable pin, then enter a for-loop that walks through the number of sensors we have, setting our byte variables to the correct values. Here the _count _variable will loop 0 through 7, with s0, s1, and s2 being set to 1 or 0 depending on the binary representation of count:

  //Enable the mux
    digitalWrite(enable,LOW);
    int numSensors = 3;

	// loop over 
    for (int count=0; count < numSensors ; count++) { //loop through each channel, checking for a signal
      
      int row = count; //channel 5 = 5th element in the bin list -> 101 etc. 
      //Read mux selector bits
      s0 = bitRead(row,0); //bitRead() -> parameter 1 = binary sequence, parameter 2 = which bit to read, starting from the right most bit
      s1 = bitRead(row,1); //channel 7 = 111, 1 = 2nd bit 
      s2 = bitRead(row,2); // third bit

Still within the for-loop, we send our binary values to our digital pins, and delay for 50ms to allow the multiplexer time to adjust its channels. We then do an analog read of the output of the multiplexer, which corresponds to one of the 8 temperature sensors we are iterating through:

      //write selector bits gates
      digitalWrite(sel2, s2); // send the bits to the digital pins 
      digitalWrite(sel1, s1);
      digitalWrite(sel0, s0);

      delay(50);

      //Grab reading through gate
      int reading = analogRead(A0);

Finally, we send our value into the modify function which type casts it to a float and multiplies it by a constant so it returns the sensors value in degrees Celsius. The value is then added to an array to later be conveyed to the pi, followed by another delay to give the system time to read the analog value:

      //Apply mod for correct temp
      float value = modify(reading);

      //Assign to array
      valuesToSend[count] = value;

      delay (50); // time to read the values
Clone this wiki locally