Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Need design and cod...
 
Share:
Notifications
Clear all

Need design and code for multiple sensors using Blynk and ESP8266

Page 2 / 2

(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

Again, everything is fine except gas and motion. Both show 0 on the SM. I'm using lighter fluid to trigger the sensor and it works because the green led on it lights when close to the fumes. The sensors are receiving the close to 5volts required so the multiplexer is working on that end.

Btw, I sent u a coffee.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 

Apologies, I did not gather that from your previous post ... 😞  and thanks for the coffee 😁 👍 

OK, so we are pretty certain the sensors work, however we're not sure if this all passes correctly through with the multiplexer. Got ya! 😊 

What multiplexer are you using?
While trying to find some more info, I found that there quite a few different approaches and IC's used for this purpose.
The smallest multiplexer I could find was the 8 port CD4051 - which requires 3 bits to be set to select a port. (source)
If you're using that one, then you'd need to set the 3rd bit as well (in your code I found only 2 pins being set high or low).

Maybe we're missing one pin?


ReplyQuote
(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

CD74HC4067 CMOS 16 Channel 16 CH Digital Analog Multiplexer Breakout Module.

That is true because I was not using the proximity sensor, which would be the 3rd pin. Again, I'm not familiar with how the multiplexer works as far signal processing so are you saying it needs all 3 sensors to work at all as currently configured? I am using C2(gas) and C1 (motion). The board is being powered by the ESP8266 vin pin, which is also supplying the power to the 2 sensors

 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 

That may be good info!
The entire schematic would have been helpful with that as well ... 😊 

So the multiplexer chooses which sensor is actually connected to the ESP/Arduino pin, based on how 4 pins (S0-S3) are being set.
Since the CD74HC4067 offers 16 channels, then this means that 4 "bits" (switches) need to be set, to determine what pin of will be used (eg what sensor we pick).

I found this list in the spec-sheet:

These "bits" are set with the S-pins of the IC (S0, S1, S2, S3 - pins 10,11, 13 and 14) in this figure:

So if we want to use the sensor on I0 (pin 9), then we need to set S0-S3 all to LOW.
If we want to use the sensor on I1 (pin 8), then we need to set S0 to HIGH and S1-S3 to LOW.
And so on ...

Now, your code only sets S0 and S1, which leaves the state for S2 and S3 to potentially be something random.

So let's look at an example:
We want to use I0 (pin 9) and in your code that would mean that we set both S0 and S1 to LOW.
Now since the state of the pins S2 and S3 are random, and we do not know what these pins may be, which results in the pin selection to be one of these;

S0 S1 S2 S3 - pin
-----------------
 0  0  0  0 -  0 
 0  0  1  0 -  4
 0  0  0  1 -  8
 0  0  1  1 -  12

(0 = LOW, 1 = HIGH)

 

So I think you will need to connect the other 2 pins to your Arduino as well and set them to LOW as well in your code, so we are 100% sure the right pin of the multiplexer is being picked.
Meaning that you'd effectively would use 5 pins (1 for the data, 4 for selecting the sensor), and you are only using 4 sensors, making the multiplexer potentially useless. 

Having said that, since you do not really need all these pins, you could consider connecting S2 and S3 straight to GND so they are always LOW.
Not sure if the specs of the IC allows for that, and if it would work properly, but I'm confident it would work an worth a try 😉 

I guess this also illustrates that it is important to provide as much detail as possible, so not just the sketch, but also the schematics of the wiring. 😊 

Personally, I'd do the test again, with pins 13 and 14 from the CD74HC4067 connected to GND. See if that gives correct results.

Note: do you use the IC directly or one of those breakout boards?
The reason I'm asking is that there is a pin "E" (pin 15), which I'm not 100% sure what it is used for and if it should be wired or not.
 

 

Note: Just for reference, I'va attached the spec-sheet of the CD74HC4067.


ReplyQuote
(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

I'm absorbing your last post fairly well, it might take another coffee to have it sink in. I'll be studying this today and experimenting. This is the lame diagram that I have to go by. No schematic. I am not using the voltage regulator because it gets too hot, over heats and shuts off. A heat sink works but the heat in general sort of defeats the thermometer. It's just powered with the usb and it is providing stable voltage. NodeMCU is on a breadboard. With the MP, I have pins directly soldered to the circuit board except the grounds where the wires are directly soldered without pins. Thanx!

 


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 

Coffee is always good 😁 

So when I look at the schematic picture, I see that S0 and S1 are not connected at all.
Is that a drawing mistake? D3 should go to S0 and D4 to S1.

I see S3 and S4 are connected to ground so we'd only need to set the bits for S0 and S1 to select a sensor - that looks OK at first glance.

As for the voltage regulator; I would prefer a simple 5V power-supply anyway. Just in case you're using batteries, I'd rather take 4x AA (4x1.5V = 6V) instead of 9V - the 7805 will then not get that hot. Also note that 4.5V may be enough as well (internally the ESP8266 runs on 3.3V if I'm not mistaken).

On that note, I've switched completely to the ESP8266 as an Arduino replacement (ESP32 is on my "to do" list), and found it to be very stable, fast and with plenty capacity to run code.


ReplyQuote
(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

I noticed that too but when the 2 gauges on Blynk were showing they were receiving a signal, albeit wonky, I assumed it was my lack of knowledge on how the multiplexer exactly works. It seemed at the time more than a coincidence that the exact pins not being used were the pins that were in the code and that having them "open" was meant to be. I'm soldering pins now to the MP to make it all easier and then I'll test.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 

Well, what you may have observed is this: The analog pin in the ESP/Arduino, when not connected at all, can still potentially produces fluctuating numbers.
A reason why these random numbers, read from an unconnected analog pin, are often used as the "seed" for the random generator.

I'm confident that connect D3 (ESP) to S0 (Multiplexre),  and D4 (ESP)  to S1 (multiplexer), could fix that problem. Fingers crossed 😊 


ReplyQuote
(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

You can sleep well tonight knowing you helped someone with the success of their project. It works!  Not having the D3 and D4 connected was the biggest issue. There were a few code issues after that with some setting changes. I will consider that battery option. I have one more sensor to add to it once I can get it working on it's own so you may hear from me on that but I learned a boatload to move forward. I'm still a bit perplexed by the multiplexer. I understand it halfway so that is a good start. Here is the finished code. I left the proximity sensor code in because I didn't want to mess with what was working. Thank you again!! 🤩

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>


/********************** Pin Definition ******************************/

//Relays for switching appliances
#define Relay1 D6
#define Relay2 D2
#define Relay3 D1
#define Relay4 D5

//DHT11 for reading temperature and humidity value
#define DHTPIN D7

//buzzer to use it for alert
#define buzzer D0

//Selection pins for multiplexer module to switch between different sensors and give data on a single analog pin.
#define Multiplexer_Pin1 D3
#define Multiplexer_Pin2 D4

//Analog pin to read the incoming analog value from different sensors.
#define Multiplexer_DataPin A0

#define Multiplexer_Gas1 LOW
#define Multiplexer_Gas2 LOW
#define Multiplexer_Motion1 HIGH
#define Multiplexer_Motion2 LOW

int proximity;
int light;
int gas;
int motion;
int Readmotion;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

char ssid[] = " ";
char pass[] = " ";
char auth[] = " ";

void sendSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature(true); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, proximity);
  Blynk.virtualWrite(V3, gas);
  Blynk.virtualWrite(V4, motion);
  Blynk.virtualWrite(V5, light);
}

void setup() {

  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(Multiplexer_Pin1, OUTPUT);
  pinMode(Multiplexer_Pin2, OUTPUT);
  pinMode(Multiplexer_DataPin, INPUT);
  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop() {

  digitalWrite(Multiplexer_Pin1, Multiplexer_Gas1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Gas2);
  gas = analogRead(Multiplexer_DataPin);
  Serial.print("Gas - ");
  Serial.println(gas);

  // Address 10
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion1);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion2);
  motion = analogRead(Multiplexer_DataPin);
  if (motion > 512) {
    motion = 1;

  } else {
    motion = 0;

  }
  // Address 01
  digitalWrite(Multiplexer_Pin1, Multiplexer_Motion2);
  digitalWrite(Multiplexer_Pin2, Multiplexer_Motion1);
  proximity = analogRead(Multiplexer_DataPin);
  Serial.print("Proximity - ");
  Serial.println(proximity);
  if (proximity < 512) {
    proximity = 1;
    digitalWrite(buzzer, Multiplexer_Motion1);
  } else {
    proximity = 0;
    digitalWrite(buzzer, LOW);
  }

  // Address 11
  digitalWrite(Multiplexer_Pin1, HIGH);
  digitalWrite(Multiplexer_Pin2, HIGH);
  int raw_light = analogRead(Multiplexer_DataPin);
  light = map(raw_light, 1024, 0, 0, 100);
  Serial.print("Light - ");
  Serial.println(light);

  Blynk.run();
  timer.run();
}

 


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 
Posted by: @locomojo

You can sleep well tonight knowing you helped someone with the success of their project. It works!  Not having the D3 and D4 connected was the biggest issue.

Awesome! That is great news! 👍 
Wow, how one can get send in a loop for something small right? 😉 

I'm happy to see that you've got it to work - and thanks for posting the sketch 😊 


ReplyQuote


(@locomojo)
Active Member
Joined: 7 months ago
Posts: 14
Topic starter  

I made the  photo-schematic to share. I did not use the proximity sensor, so I didn't include it.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 2027
 

Awesome! Thanks for sharing 😊 👍 

I quite often use Fritzing for this - it's only €8 and well worth it.
If you'd like to try it first, you may have to look for an older free download (no longer offered on the Fritzing website).
Or try if the code at Github can be compiled (could be a pain to do).


ReplyQuote
Page 2 / 2

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: