cancel
Showing results for 
Search instead for 
Did you mean: 

Bluepill HC4067 via Arduino IDE

CBuet.1
Associate II

Hi everyone. I´d like to use a HC4067 mulitplexer to expand analog inputs on a bluepill. I´m using Arduino IDE to program the thing. But I can´t get reliable results. The values are jumping up and down or not reacting to input (potentiometer).

I´m wondering if using a HC4067 is restricted to certian pins?? I´m trying to use:

int s0 = PB4;

int s1 = PB3;

int s2 = PA15;

int s3 = PA12 ;

SIG = PA4;

I´m powering the HC4067 with 3.3 V and have grounded all inputs except input 1. I get fluctuating readings and POT on input 1 isn´t doing much, although I should see 0-4096.

Any help appreciated. Keep in mind ... newbie here. Or father with just occasional spare time trying to build an RC transmiiter for his son.

Before you ask: I use these pins because there are already other things going on on the board...NRF24 transmitter, Oled, etc.

This code is just a test setup.

//Mux control pins
int s0 = PB4;
int s1 = PB3;
int s2 = PA15;
int s3 = PA12 ;
 
//Mux in "SIG" pin
int SIG_pin = PA4;
 
 
void setup() {
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
 
  Serial.begin(9600);
}
 
 
void loop() {
 
  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  for (int i = 0; i < 16; i ++) {
    Serial.print("Value at channel ");
    Serial.print(i);
    Serial.print("is : ");
    Serial.println(readMux(i));
    delay(100);
  }
 
}
 
 
int readMux(int channel) {
  int controlPin[] = {s0, s1, s2, s3};
 
  int muxChannel[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };
 
  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  }
 
  //read the value at the SIG pin
  int val = analogRead(SIG_pin);
 
  //return the value
  return val;
}

18 REPLIES 18
Javier1
Principal
  //loop through the 4 sig
  for (int i = 0; i < 4; i ++) {
    digitalWrite(controlPin[i], muxChannel[channel][i]);
  //read the value at the SIG pin
  int val = analogRead(SIG_pin);
  }
 

try this

we dont need to firmware by ourselves, lets talk
CBuet.1
Associate II

tried. Now I get near stable 0 on all inputs. If I let just one input pin on the HC4067 float I get garbage on all inputs. Pulling a pin to 3.3V does noting.

CBuet.1
Associate II

BTW the orignal code, of course with other pins (S0-3/8-11) and SIG A0, works perfect on a Nano. Values are 0-1023 on input 1. all others stay put at 0.

well that is something, did you discarded wiring problems? you could share your wiring with us

we dont need to firmware by ourselves, lets talk

arduino nano is based on ATmega328 thats a 5v microcontroller.

bluepill is based on stm32f103c8 and its a 3v3 microcontroller.

Maybe your Multiplexer is not recognising your digital signals as high, you could try changing the multiplexer vcc down to 3v3.

we dont need to firmware by ourselves, lets talk

for testing purposes I just moved the S0-4 and SIG to a Nano and it worked as expected. I admit It is a breadboard mess, but I´ve been checking the wiring for hours now. Thats why my initial thought was I probably could not use

int s0 = PB4;

int s1 = PB3;

int s2 = PA15;

int s3 = PA12 ;

I´ve been running the STM setup and the multiplexer solely on 3V3. Althtough the pins I use o the STM are 5V tolerant.

5v tolerant means they will accept 5v input without burning, they wont do 5v output.

This looks like a wiring problem or a Voltage gpio problem.

You can try reading adc inputs with the stm32 and doing de digital logic with wires to 3v3, to corroborate this.

we dont need to firmware by ourselves, lets talk

it is really weird. I´ve redone the whole wiring and not matter what I try it isn´t working. Right now I have a POT only on Channel 15 and all others to ground. But the readings of all channels are affected by the POT. Could it be a similar issue to what I had with an i2C display? Had to add 4K7 pullups before it wanted to display anything.