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

Could you share your wiring? maybe a paint drawing?

we dont need to firmware by ourselves, lets talk

0693W000008xBC3QAM.jpg

I´ve been using Atmels for years, although I never became realy good at coding. Its just a hobby. Those STMs are giving me headaches though. I had this Bluepill and a really strange Blackpill (strange pin layout) laying around for a while and now wanted to build an RC excavator remote for my son. An nrf24l will be taking away some of the analog inputs and I still need 8 analog pins (2 3-axis joysticks, 1 2 axis-joystick). That´s why I need to expand the ADC inputs. Alternatively I´ll revert to using a Mega (because of available memory for an Oled).

more accurate representation0693W000008xBOdQAM.jpg

This one. Damn. Of course those pins aren´t floating. 0693W000008xBRSQA2.jpg

What about using a DELAY(1); in between mux change and adc reading?

In case you surrender with the multiplexer, there is 10 adc capable pins in bluepill!!!

You could change SPI or UART to other ports so you can use all of the adc pins.

0693W000008xFG9QAM.png

we dont need to firmware by ourselves, lets talk

Thanks so much for taking your time Javier. Somehow it did not cross my mind to use the other SPI pins. I have used a different pinout diagram which did not list the SPI pins. Now I need to figure out where to change the pin assignment in the library.

And yes ... 10 ADCs is what I need. I´ll give it a try. again ... thank you very much!

Javier ... I´ve dug through a lot of info and managed to swap pins to use SPI1 alernate pins unsing

afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY);

afio_remap(AFIO_REMAP_SPI1);

in setup()

but it seems it conflicts with the i2c bus (Oled display) and I havent been able to find a solution. I´ve read examples where people have the same issue with an SD reader and they get away with calling SD.begin from within the loop. But this doesn´t work with u8g2 library.

In another post somewhere I´ve read that i2c clock must be disabled, but I have no clue how to do that.

sorry you missed me there, i have never seen "afio_cfg_debug_ports()".

in case youre still fighting there is also more than one i2c port...

we dont need to firmware by ourselves, lets talk