Expanding Arduino Pin W - DEMUX IC 4051

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

11th May 2016 06 #arduSerie - Expanding Arduino's


Pin w/ DEMUX IC 4051 !!
DEMUX [https://en.wikipedia.org/wiki/Multiplexer] IC 4051

[https://2.bp.blogspot.com
/-AXWMewM_Ohc/VzSZCc0mk9I/AAAAAAAACpI/yypMN8v-y-UVxAT-
jUSd1aTmxnTdywo3wCLcB/s1600/4051_gif.gif]

Bi-directional device. It allows you to expand the number of pins available for
input and output.

MUX: MUltipleXing is choosing from one of varios lines (one at a time) and
forwards its contents
down a single line .

[https://encrypted-tbn1.gstatic.com
/images?q=tbn:ANd9GcS4YOvGpCHReJVvDuWwaoEprNhIY9kYAAlcT-
7T9GfWZflmzOxhuA]

DE: DEmultiplexer is doing the reverse operation; a single input line is fowarded
to one of many output lines.

demultiplexer symbol

1 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

[https://3.bp.blogspot.com/-5CVXlN41LME/VzPZ-Jb_J9I/AAAAAAAACoE
/3FL4_O_oAWwSTuBHGim9HOd1lErC27rewCLcB/s1600/DigRotarySwitch.jpg]
The control of this IC is simple to implement. In this tutorial we started a
collection of codes and ways of making the chip work well with arduino. The
concept is simple. follow the slides to quickly understand the workings of this
fabulous IC: CD4051!!!

Controller keeps under systematic scanning the s0, s1 and s2 pins from IC, as it
was a security camera; As soon as outside signal is sensing, physically selects
the appropriate channel and prepares the circuit to read/write from/to a route
door on/to outside signal, by setting/reading a variable control.

We will study four types of code that the community has used to interface with
the Arduino; watch the video and download the code [https://github.com/giljr
/Ardu_Serie] and study line by line:

1) _06_arduSerie_sketch_01.ino: Reading multiple LDR (light dependent


resistors) using a single analog port. We show only the multiplexing (many to
one) obtained by sensors CdS analog signal in sequence. It is a need to check
the output in the serial monitor arduino. Pretty simple, just to learn the basics.
Application: you can put a light sensor in each room of your home and save the
data in the database. Use the internet shield provided by Arduino. Note the part
of the code: digitalWrite (addressX (which & y) HIGH: LOW).

In our analogy, the input controller (code loaded in arduino) monitors (ternary
operator) the camera and realizes the input of an element, and quickly connects
(digitalWrite ()) of the appropriate channel;
ready, the trail is open to the external signal we forward to our building. Now only
reads the information in the common gateway (return analogRead(common);-)

// Example of using the 74HC4051 multiplexer/demultiplexer

// Author: Nick Gammon

// Date: 14 March 2013

const byte common = A0; // where the multiplexer in/out port is connected

// the multiplexer address select lines (A/B/C)

const byte addressA = 6; // low-order bit

const byte addressB = 5;

2 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

const byte addressC = 4; // high-order bit

void setup ()

Serial.begin (115200);

Serial.println ("Starting multiplexer test ...");

pinMode (addressA, OUTPUT);

pinMode (addressB, OUTPUT);

pinMode (addressC, OUTPUT);

} // end of setup

int readSensor (const byte which)

// select correct MUX channel

digitalWrite (addressA, (which & 1) ? HIGH : LOW); // low-order bit

digitalWrite (addressB, (which & 2) ? HIGH : LOW);

digitalWrite (addressC, (which & 4) ? HIGH : LOW); // high-order bit

// now read the sensor

return analogRead (common);

} // end of readSensor

void loop ()

// show all 8 sensor readings

for (byte i = 0; i < 8; i++)

Serial.print ("Sensor ");

Serial.print (i);

Serial.print (" reads: ");

Serial.println (readSensor (i));

delay (1000);

} // end of loop

2) _06_arduSerie_sketch_02.ino: This uses library to abstract away a few of the


details to do with analog multiplexing and demultiplexing. We show only the
demultiplexing (one to many) transmitting signal to each of the 8 LEDs in
sequence. Pretty simple too. Application: you can control various actuators
monitoring a signal. Here, the work we have is only configure which pins will be
the monitor and what is the main gate (AnalogDeMux admux (x, y, z,

3 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

WRITEPIN));
Realize you as the code is clean. Congratulations to ajfisher git
[https://github.com/ajfisher/arduino-analog-multiplexer] and thanks for masterpiece
code !

#include "analogmuxdemux.h"

#define WRITEPIN A0 // connect to a PWM pin in order to be able to output an

analog signal

#define NO_PINS 8 // how many output pins are you going to use on the DeMux?

This exmample uses 3.

// set up the DeMux ready to be used. Watch the order of S0, S1 and S2.

AnalogDeMux admux(4, 5, 6, WRITEPIN);

void setup() {

pinMode(WRITEPIN, OUTPUT);

delay(1);

void loop() {

// go through each pin in turn and then just light it up like a dimmer

for (int pinno = 0; pinno < NO_PINS; pinno++) {

admux.SelectPin(pinno); // choose the pin you want to send signal to off the

DeMux

for (int i = 0; i < 255; i++) {

admux.AnalogWrite(i); // simply pass an analog value as per normal.

delay(1);

for (int i = 255; i >= 0; i--) {

admux.AnalogWrite(pinno, i); // different way of using analogWrite specifying

pin

delay(1);

3) _06_arduSerie_sketch_03.ino: The official means the arduino


[http://playground.arduino.cc/Learning/4051] work with the 4051 (de)multiplexer. We
will Compare just how the signal is captured and written in the control pins. Here
the interest is only didactic, and you can choose the best approach. This code
follows the Cartesian thinking, divide and then analyzes( rx = bitRead(count, a)
& digitalWrite(y, rx)); choosing the best approach is personal matter; I prefer the
gammon [http://www.gammon.com.au/forum/?id=11976] code; you choose your
option!! All work out !!! It's up to you !!!

/*

4 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

codeexample for useing a 4051 * analog multiplexer / demultiplexer

by david c. and tomek n.* for k3 / malm� h�gskola

edited by Ross R.

*/

#define NO_PINS 8 //how many output pins

int r0 = 0; //value of select pin at the 4051 (s0)

int r1 = 0; //value of select pin at the 4051 (s1)

int r2 = 0; //value of select pin at the 4051 (s2)

int common = A0; //which y pin we are selecting

void setup() {

pinMode(4, OUTPUT); // s0

pinMode(5, OUTPUT); // s1

pinMode(6, OUTPUT); // s2

void loop () {

for (int count = 0; count <= 8; count++) {

// select the bit

r0 = bitRead(count, 0); // use this with arduino 0013 (and newer versions)

r1 = bitRead(count, 1); // use this with arduino 0013 (and newer versions)

r2 = bitRead(count, 2); // use this with arduino 0013 (and newer versions)

//r0 = count & 0x01; // old version of setting the bits

//r1 = (count>>1) & 0x01; // old version of setting the bits

//r2 = (count>>2) & 0x01; // old version of setting the bits

digitalWrite(4, r0);

digitalWrite(5, r1);

digitalWrite(6, r2);

//Either read or write the multiplexed pin here

analogWrite(common, 255);

delay(100);

4) _06_arduSerie_sketch_04.ino: We show Knight Rider LED Light Bars for


you!!! Only to play a little !!! Finally this code uses an array and walks on it...Very
elegant, also!!

/*

codeexample for useing a 4051 * analog multiplexer / demultiplexer

by david c. and tomek n.* for k3 / malmö högskola

5 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

*/

int route = A0; //just a route

int r0 = 0; //value select pin at the 4051 (s0)

int r1 = 0; //value select pin at the 4051 (s1)

int r2 = 0; //value select pin at the 4051 (s2)

int row = 0; // storeing the bin code

int count = 0; // just a count

int bin [] = {000, 1, 10, 11, 100, 101, 110, 111};//bin = binär, some times it is so

easy

void setup() {

pinMode(4, OUTPUT); // s0

pinMode(5, OUTPUT); // s1

pinMode(6, OUTPUT); // s2

// digitalWrite(led, HIGH);

Serial.begin(9600);

void loop () {

for (count = 0; count <= 7; count++) {

row = bin[count];

r0 = row & 0x01;

r1 = (row >> 1) & 0x01;

r2 = (row >> 2) & 0x01;

digitalWrite(4, r0);

digitalWrite(5, r1);

digitalWrite(6, r2);

analogWrite(route, 255); // different way of using analogWrite specifying pin

delay(100);

for (count = 7; count >= 0; count--) {

row = bin[count];

r0 = row & 0x01;

r1 = (row >> 1) & 0x01;

r2 = (row >> 2) & 0x01;

digitalWrite(4, r0);

digitalWrite(5, r1);

digitalWrite(6, r2);

analogWrite(route, 255); // different way of using analogWrite specifying pin

6 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

delay(100);

The idea is to know the background components that will help us in a great
project! Let's step by step. Accumulating knowledge and technology !!! This time
4051 DEMUX !!! This will be the technology that drives us to the level of great
inventors, just playing and being happy with electronics !!!

Watch the video now!!! thank you for the privilege to have you here !!!

Notes:
(i) In the laboratory, inadvertently, on code 1, I changed the MSB and LSB
connections order, S0. S1 and S2; What we observe is that the distribution
between the pins began to switch to the pins 13, 1, 15, 2, 14, 5, 12, and 4; unlike
as in the datasheet [https://www.google.com.br/url?sa=t&rct=j&q=&esrc=s&
source=web&cd=14&cad=rja&uact=8&ved=0ahUKEwj9-
Pvb4tfMAhWGhZAKHc6VClMQFgh0MA0&url=http%3A%2F
%2Fwww.ti.com%2Fproduct%2FCD4051B&
usg=AFQjCNHzmumoWtgRDcL6PlH6vomaLN7CkA&sig2=vSxMNDuaBbTD9JXe7IVAxA]
that gives the sequence 13, 14, 15, 12, 1, 5, 2, and 4 (see below). Although this
was noticed after recording, I decided to keep as is and add this observation; the
code works, although in another sequence. If you do not approve, change the
sequence between pins 11,10 and 9 and ready to give the sequence according
to the datasheet; others codes, 2-4, are as datasheet says.

[https://www.google.com.br
/url?sa=t&rct=j&q=&esrc=s&source=web&cd=14&cad=rja&uact=8&ved=0ahUKEwj9-
Pvb4tfMAhWGhZAKHc6VClMQFgh0MA0&url=http%3A%2F
%2Fwww.ti.com%2Fproduct%2FCD4051B&
usg=AFQjCNHzmumoWtgRDcL6PlH6vomaLN7CkA&sig2=vSxMNDuaBbTD9JXe7IVAxA]

(ii)Another observation concerns the difference between digitalRead x


analogRead /
digitalWrite and analogWrite; see the comparison table below;

7 of 8 14/04/2023, 22:07
06 #arduSerie - Expanding Arduino's Pin w/ DEMUX IC 4051 !! | Jungl... http://jungletronics.blogspot.com/2016/05/06-arduserie-expanding-ardu...

Function Equivalent
digitalRead(A0) = 1 analogRead() if > 512
digitaWrite(A0, 0) = 0 analogWrite(A0, 0) = 0
digitalWrite(A0, 1) AnalogWrite(A0, 255)
works only on analog pins; it
analogRead(x) can take any x value from 0 >
1023
works on all analog* and
analogWrite(A0,x) PWM** pins; it can take any x
value from 0 > 255
*analog pins use ADC 10-bit (hence, 2^10 = 1024-1)
**PWM pins use Timer0 (8-bit pins 6 & 5)
Timer1 (16-bit pins 9 & 10) and Timer2 (8-bit pins 11 &
3) Arduino UNO

(iii) It is that if we call the mixed methods, digital / analog, Arduino API will not
work predictably;
that was the reason it did not work in my lab the code in this link [http://dawson-
station.blogspot.com.br/2010/01/mux-demux-cd4051-parlor-tricks.html] ...but that's
another story ...
Until next experience !!!

"What I cannot create, I do not understand" - Richard Feynman

Postado há 11th May 2016 por J3

0 Adicionar um comentário

8 of 8 14/04/2023, 22:07

You might also like