Today, in this tutorial, we are going to interface 74HC4051 Multiplexer/Demultiplexer with STM32. I will cover the multiplexing part first.
what is a multiplexer
Well, basically multiplexer is a device that can take multiple inputs, and have only one output. At some random time, the output of the mux is connected to only one input. And which input it must be connected to, can be selected by using select pins.
74HC4051 is a 8:1 mux, that means that it have 8 input pins (Y0 to Y7), and 1 output pin (Z). To control these 8 input pins, it have 3 select pins (S0 to S2). You can see that in the picture below.
How does it work
The working of a mux is pretty simple, and it remains same for other multiplexers too.
Enable pin (E) is an active low pin. It means that the device will be enabled whenever the pin is low. Now, as shown above, the combination of select pins must be pulled low to connect the respective input pin (Y) to the output (Z).
To show the working, I am going to connect 3 ADC sensors to the input pins (Y0 to Y2), and I will read the sensor values from the output pin (Z). Below is the setup of my cube MX.
As you can see 3 select pins are connected to PA4, PA5, and PA6 and enable pin is connected to PA7. I have also renamed them accordingly.
Now, as i am reading the ADC sensor value, the Z pin is connected to the ADC CHANNEL 1 i.e. PA1.
This is it for the setup, now let’s take a look at the coding part.
Some insight into the code
NOTE:- The reference of some of the functions is taken from sparkfun.com and the link to the code is https://learn.sparkfun.com/tutorials/multiplexer-breakout-hookup-guide/all
I have defined
selectPins as an array of the select pins for simplicity.
selectMuxPin is used to select the respective pin of the mux as input pin. Basically it connects the respective Y pin to the Z pin. For eg-
selectMuxPin(2) will connect Y2 to Z pin and we can read the value of the sensor connected to the Y2 pin.
The argument of the
selectMuxPin i.e. pin, can take the values from 0 to 7.
First of all,we have to pull the enable pin (E) LOW, so as to enable the mux.
Also set all the select pins as HIGH, so that we can select the respective mux pin by pulling them (select pins) low.
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
We are going to read the data using pollforconversion, as it’s the easiest way to read ADC value. So basically the steps are as follows
- Select the respective pin of the mux that you want to read
- Start the ADC
- Poll for conversion to finish
- Read the value of the sensor
- Stop the ADC
Using 74HC4051 as DeMultiplexer
Opposite to mux, a demultiplexer is a device that takes only one input, and that can be connected to different outputs, again based on the select pins. 74HC4051 can be used as 1:8 Demux. The block diagram is shown below
I am using the same functions as above to
selectPins or to
selectMuxPin. The only changes here will be regarding the Z pin. This time Z pin is selected as output in the CubeMX.
So, all the Y pins are connected to the LEDs. And when the Z pin is pulled low, the respective Y pin is also pulled low, and the LED connected will turn ON. In the above code, the output pin of the DEMUX changes every 250 ms, and Z pin is again pulled low.
- Interface LCD 16×2 with STM32 without I2C January 21, 2020
- Interface DS3231 RTC module with STM32 January 13, 2020
- How to use Binary Semaphore in STM32 January 11, 2020
- Managing Multiple UARTs in STM32 December 18, 2019
- Multiplexer 74HC4051 and STM32 December 6, 2019
- Free RTOS Tutorial 2.0 with STM32 November 27, 2019