'16-channel multiplexer & python
Update 4/5/22 Equipment: ESP8266 board, CD74HC4067 16-Channel Multiplexer
Code Platform: micropython using Thonny on RPi transmitting over USB cable.
Project Goal: 16 individual photo-resistors to trigger individual LEDs or Relays.
Code: Opening Line...
from machine import Pin, ADC
from time import sleep_ms
s0 = Pin(16, Pin.OUT) # assigns s0 on multiplexer to D0 pin on ESP8266
s1 = Pin(5, Pin.OUT) # D1
s2 = Pin(4, Pin.OUT) # D2
s3 = Pin(2, Pin.OUT) # D4
SIG_pin = ADC(0) # assigns SIG on multiplexer to A0 pin on ESP8266
to test the code, I ran
val = Sig_pin.read()
print(val)
and I get a value which reflects an output... i'm not sure from which of the (16) channels. If i comment 3 out of 4 of the s# pins, I get a change in value as expected... but again, I'm not sure which channel from the active pin.
I have thought about it and I can create a binary directory to use for the channels.
mux = [{0b0000},{0b0001},{0b0010},{0b0011}],[{0b0100},{0b0101},{0b0110},{0b0111}],[{0b1000},{0b1001},{0b1010},{0b1011}],[{0b1100},{0b1101},{0b1110},{0b1111}]
In arduino, i could use digitalWrite(pin,channel) to call for a specific channel, but how can I do this in python?
and then how can I get the Sig_pin to read that specific pin & channel?
Solution 1:[1]
Solved the problem. I was overthinking the process way too much.
ch1 = (s0.value(0), s1.value(0), s2.value(0), s3.value(0))
val = Sig_pin.read()
print(val)
Setting each of the s# pins to a high low state gives me my binary combination.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | box-o-rocks |
