BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

DeMux for Arduiono 74HC4067 with Wemos Mini, probel with setting digital pins

Hello,
I have a problem with my project and I require some help please. Im using a wemos D1 mini and I want to measure multiple analog signals. For that I’m using 74HC4067 16 channel mux.
To activate a specific channel I have to set some digitial Pins to high or low.

I would like to set digital Pins in “void sensorData()”, save the value and send it to a virtual pin to blynk. Then activate another channel and save it to anoter virtual pin. But it does not work. I only get values on my Analog pin, if I set the values for the digital pins in void loop(). If I try to set the digital pins inside “void SensorData()” it’s not working
Here is a part of my code which does not work?

void sendSensor()
{
  digitalWrite(MUXPinS3, LOW);
  digitalWrite(MUXPinS2, LOW);
  digitalWrite(MUXPinS1, LOW);
  digitalWrite(MUXPinS0, LOW);
  int sensorData = analogRead(A0); //reading the sensor on A0
  sensorData = map (sensorData,0, 1023, 100, 0);
  Blynk.virtualWrite(V8, sensorData); //sending to Blynk
  Serial.println("Sensor 1: ");
  Serial.println(sensorData);
  }

void loop(){  Blynk.run();       }

Thank you for your help

I don’t think that I can help you, but others might if you post your complete sketch!

Here the code

/**************************************************************
Measuring Moisture for Plants

Sending E-Mail
}
 **************************************************************/
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ""; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";  //Enter your WIFI Name aterm-9fdb33-g
char pass[] = "";  //Enter your WIFI Password 5b62bf4ca241


//Mux control pins
int MUXPinS0 = D0;
int MUXPinS1 = D1;
int MUXPinS2 = D2;
int MUXPinS3 = D3;



BlynkTimer timer;

//unsigned long EmailTimer=0;


void sendSensor()
{
  digitalWrite(MUXPinS0,LOW)
  digitalWrite(MUXPinS1,LOW)
  digitalWrite(MUXPins2,LOW)
  digitalWrite(MUXPinS3,LOW)
  int sensorData = analogRead(A0); //reading the sensor on A0
  sensorData = map (sensorData,0, 1023, 100, 0);
  Blynk.virtualWrite(V8, sensorData); //sending to Blynk
  Serial.println("Sensor 1: ");
  Serial.println(sensorData);
  digitalWrite(MUXPinS0,HIGH)
  digitalWrite(MUXPinS1,HIGH)
  digitalWrite(MUXPins2,LOW)
  digitalWrite(MUXPinS3,LOW)
  int sensorData1 = analogRead(A0); //reading the sensor on A0
  sensorData = map (sensorData1,0, 1023, 100, 0);
  Blynk.virtualWrite(V9, sensorData1); //sending to Blynk
  Serial.println("Sensor 2: ");
  Serial.println(sensorData)
  }

void setup()
{
  pinMode(MUXPinS0, OUTPUT); 
  pinMode(MUXPinS1, OUTPUT); 
  pinMode(MUXPinS2, OUTPUT); 
  pinMode(MUXPinS3, OUTPUT); 

  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
 
  timer.setInterval(6000L, sendSensor); // Setup a function to be called every 6 second

}

void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

What are you seeing in your serial monitor when you run this code?

You’re mixing-up your sensorData and sensorData1 variables in the second part of your function…

but that shouldn’t stop you getting some readings at least.

Pete.

You are right. There is small error. But that is not important. I get some reading but without any sense. The values are far from what they should be. If I copy this part:

  digitalWrite(MUXPinS0,LOW)
  digitalWrite(MUXPinS1,LOW)
  digitalWrite(MUXPins2,LOW)
  digitalWrite(MUXPinS3,LOW)

to “void loop”, I get correct readings.

Why don’t you want to share your serial monitor output?

Pete.

Random number it seems:

   ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on ESP8266

[4488] Connecting to blynk-cloud.com:80
[5077] Ready (ping: 20ms).
Sensor 1: 
100
Sensor 2: 
7
Sensor 1: 
100
Sensor 2: 
8
Sensor 1: 
100
Sensor 2: 
7

Is this with the code exactly as you posted it, or did you fix the sensorData/sensorData1 issue first?

Pete.

Hey Pete,
it was after fixing the code.
I worked on the code and I thing I solved the problem. I’m not sure what the problem was, but it’s solved. I’m setting the digital pins and reading the analog pin in a subfunction now. Down below is the code.
Now I have another question. Im saving the analog signal in an array[i]. This data I would like to save to virtual pin V1 to V[i]. Right now I’m doing it with a “if” statement. Is it possible to do it with a loop?
Thank you very much.

/**************************************************************
Measuring Moisture for Plants

Sending E-Mail
}
 **************************************************************/
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
//#include <SimpleTimer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = ""; //Enter the Auth code which was send by Blink

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";  //Enter your WIFI Name aterm-9fdb33-g
char pass[] = "";  //Enter your WIFI Password 5b62bf4ca241


//Mux control pins
int MUXPinS0 = D4;
int MUXPinS1 = D1;
int MUXPinS2 = D2;
int MUXPinS3 = D3;

int myArray[16];
int pin; 

BlynkTimer timer;

//unsigned long EmailTimer=0;


void sendSensor()
{
        for (int i = 0; i < 2; i++)
    {
      myArray[i]= getAnalog(i);
      myArray[i] = map (myArray[i],0, 1023, 100, 0);
      if (i==0)
      {pin = V0;
      }
      if (i==1)
      {pin =V1;
      }
      Blynk.virtualWrite(pin, myArray[i]); //sending to Blynk
      Serial.print("Sensor 1: ");
      Serial.print(myArray[0]);
      Serial.print(", ");
      Serial.print("Sensor 2: ");
      Serial.println(myArray[1]);

    }


  }

void setup()
{
  pinMode(MUXPinS0, OUTPUT); 
  pinMode(MUXPinS1, OUTPUT); 
  pinMode(MUXPinS2, OUTPUT); 
  pinMode(MUXPinS3, OUTPUT); 
  

  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass);
 
  timer.setInterval(6000L, sendSensor); // Setup a function to be called every 6 second

}

void loop()
{
  
      Blynk.run(); // Initiates Blynk
      timer.run(); // Initiates SimpleTimer   

}
float getAnalog(int MUXyPin) {
  //MUXyPin must be 0 to 15 representing the analog pin you want to read
  //MUXPinS3 to MUXPinS0 are the Arduino pins connected to the selector pins of this board.
  digitalWrite(MUXPinS3, HIGH && (MUXyPin & B00001000));
  digitalWrite(MUXPinS2, HIGH && (MUXyPin & B00000100));
  digitalWrite(MUXPinS1, HIGH && (MUXyPin & B00000010));
  digitalWrite(MUXPinS0, HIGH && (MUXyPin & B00000001));
  return (float)analogRead(A0);
}

Yes. The ‘V’ is optional in the Blynk.virtualWrite(vPin,value) command, so you can simply do Blynk.virtualWrite(i, array[i])

Pete.