How do I select Sensor using chip 74HC138 on Wemos D1 Mini?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have a project for my graduate degree.



It's about Monitoring Temperature and Humidity using Message Queuing Telemetry Transport on Serial NodeJS. To get sensor data to Wemos D1 Mini and NodeJS had done, also on my MQTT had done. My lecturer wants to get data from two sensors at the edges of the room, he suggests using a 74HC138. I'm using this 74HC138 Datasheet and I'm using this BME280 Library.



This is my schematic:



Schematic



This is my code:



#include <BME280.h>

const uint32_t SERIAL_SPEED = 115200;
const uint8_t SCK_PIN = D5;
const uint8_t MISO_PIN = D6;
const uint8_t MOSI_PIN = D7;
const uint8_t SPI_CS1_PIN = 15;
const uint8_t SPI_CS2_PIN = 14;
const uint8_t GPIO[3] = D1, D2, D3;

static int32_t temperature, humidity, pressure;
int i;

BME280_Class BME280;

void setup()
Serial.begin(SERIAL_SPEED);
pinMode(SCK_PIN, OUTPUT);
pinMode(MOSI_PIN, OUTPUT);
pinMode(MISO_PIN, INPUT);

SPI.begin();
SPI.setBitOrder(LSBFIRST);

Serial.println(F("condition before loop = "));
//status_pin();
for (i = 0; i < 3; i++)
pinMode(GPIO[i], INPUT);
digitalWrite(GPIO[i], HIGH);

pinMode(SPI_CS1_PIN, OUTPUT);
pinMode(SPI_CS2_PIN, OUTPUT);

Serial.println(F("Starting Hardware SPIDemo example program for BME280"));
Serial.print(F("- Initializing BME280 sensorn"));

if ( (!BME280.begin(SPI_CS1_PIN) && (!BME280.begin(SPI_CS2_PIN) )
Serial.println(F("- Unable to find BME280. Waiting 3 seconds."));
delay(3000);


BME280.mode(SleepMode);
Serial.print(F("- Sensor detected in operating mode ""));
Serial.print(BME280.mode());
Serial.println(F(""."));
if (BME280.mode() == 0)
Serial.print(F("- Turning sensor to normal mode, mode is now ""));
Serial.print(BME280.mode(NormalMode));
Serial.println(""");

Serial.println(F("- Setting 16x oversampling for all sensors"));
BME280.setOversampling(TemperatureSensor, Oversample16);
BME280.setOversampling(HumiditySensor, Oversample16);
BME280.setOversampling(PressureSensor, Oversample16);
Serial.println(F("- Setting IIR filter to maximum value of 16 samples"));
BME280.iirFilter(IIR16);
Serial.println(F("- Setting time between measurements to 1 second"));
BME280.inactiveTime(inactive1000ms);
Serial.print(F("- Each measurement cycle will take "));
Serial.print(BME280.measurementTime(MaximumMeasure) / 1000);
Serial.println(F("ms.nn"));


void loop()
static uint8_t loopCounter = 0;
byte pin1 = 0x74;
byte pin2 = 0x71;

if ( BME280.begin(SPI_CS1_PIN) == 1 )
selpin(pin1);
Serial.println(F(" Data Bme = "));
else if ( BME280.begin(SPI_CS2_PIN) == 1 )
selpin(pin2);
Serial.println(F(" Data Bme = "));
printdata();


if (++loopCounter % 10 == 0)
Serial.print(F("n- Turning "));
if (BME280.getOversampling(HumiditySensor) == 0)
BME280.setOversampling(HumiditySensor, Oversample16);
Serial.print(F("ON"));
else
BME280.setOversampling(HumiditySensor, SensorOff);
Serial.print(F("OFF"));

Serial.println(F(" humidity sensing"));
Serial.print(F("- Each measurement cycle will now take "));
Serial.print(BME280.measurementTime(MaximumMeasure) / 1000.0);
Serial.println(F("ms.n"));


Serial.println(F("condition after loop = "));
//status_pin();
Serial.println();



float altitude(const float seaLevel = 1013.25)
int32_t temp, hum, press;
BME280.getSensorData(temp, hum, press);
float Altitude = 44330.0 * (1.0 - pow(((float)press / 100.0) / seaLevel, 0.1903));
return (Altitude);


void printdata ()
BME280.getSensorData(temperature, humidity, pressure);
Serial.print(F("Temperature : "));
Serial.print(temperature / 100.0);
Serial.println(F(" *C"));
if (BME280.getOversampling(HumiditySensor) != 0)
Serial.print(F("Humidity : "));
Serial.print(humidity / 100.0);
Serial.println(F(" %"));

Serial.print(F("Pressure : "));
Serial.print(pressure / 100.0);
Serial.println(F(" hPa"));
Serial.print(F("Altitude : "));
Serial.print(altitude());
Serial.println(F(" m"));
Serial.println();
delay(5000);



Serial output always says Unable to find BME280. Waiting 3 seconds. I know it's because CS pin cannot detect. I think something is missing my code.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a project for my graduate degree.



    It's about Monitoring Temperature and Humidity using Message Queuing Telemetry Transport on Serial NodeJS. To get sensor data to Wemos D1 Mini and NodeJS had done, also on my MQTT had done. My lecturer wants to get data from two sensors at the edges of the room, he suggests using a 74HC138. I'm using this 74HC138 Datasheet and I'm using this BME280 Library.



    This is my schematic:



    Schematic



    This is my code:



    #include <BME280.h>

    const uint32_t SERIAL_SPEED = 115200;
    const uint8_t SCK_PIN = D5;
    const uint8_t MISO_PIN = D6;
    const uint8_t MOSI_PIN = D7;
    const uint8_t SPI_CS1_PIN = 15;
    const uint8_t SPI_CS2_PIN = 14;
    const uint8_t GPIO[3] = D1, D2, D3;

    static int32_t temperature, humidity, pressure;
    int i;

    BME280_Class BME280;

    void setup()
    Serial.begin(SERIAL_SPEED);
    pinMode(SCK_PIN, OUTPUT);
    pinMode(MOSI_PIN, OUTPUT);
    pinMode(MISO_PIN, INPUT);

    SPI.begin();
    SPI.setBitOrder(LSBFIRST);

    Serial.println(F("condition before loop = "));
    //status_pin();
    for (i = 0; i < 3; i++)
    pinMode(GPIO[i], INPUT);
    digitalWrite(GPIO[i], HIGH);

    pinMode(SPI_CS1_PIN, OUTPUT);
    pinMode(SPI_CS2_PIN, OUTPUT);

    Serial.println(F("Starting Hardware SPIDemo example program for BME280"));
    Serial.print(F("- Initializing BME280 sensorn"));

    if ( (!BME280.begin(SPI_CS1_PIN) && (!BME280.begin(SPI_CS2_PIN) )
    Serial.println(F("- Unable to find BME280. Waiting 3 seconds."));
    delay(3000);


    BME280.mode(SleepMode);
    Serial.print(F("- Sensor detected in operating mode ""));
    Serial.print(BME280.mode());
    Serial.println(F(""."));
    if (BME280.mode() == 0)
    Serial.print(F("- Turning sensor to normal mode, mode is now ""));
    Serial.print(BME280.mode(NormalMode));
    Serial.println(""");

    Serial.println(F("- Setting 16x oversampling for all sensors"));
    BME280.setOversampling(TemperatureSensor, Oversample16);
    BME280.setOversampling(HumiditySensor, Oversample16);
    BME280.setOversampling(PressureSensor, Oversample16);
    Serial.println(F("- Setting IIR filter to maximum value of 16 samples"));
    BME280.iirFilter(IIR16);
    Serial.println(F("- Setting time between measurements to 1 second"));
    BME280.inactiveTime(inactive1000ms);
    Serial.print(F("- Each measurement cycle will take "));
    Serial.print(BME280.measurementTime(MaximumMeasure) / 1000);
    Serial.println(F("ms.nn"));


    void loop()
    static uint8_t loopCounter = 0;
    byte pin1 = 0x74;
    byte pin2 = 0x71;

    if ( BME280.begin(SPI_CS1_PIN) == 1 )
    selpin(pin1);
    Serial.println(F(" Data Bme = "));
    else if ( BME280.begin(SPI_CS2_PIN) == 1 )
    selpin(pin2);
    Serial.println(F(" Data Bme = "));
    printdata();


    if (++loopCounter % 10 == 0)
    Serial.print(F("n- Turning "));
    if (BME280.getOversampling(HumiditySensor) == 0)
    BME280.setOversampling(HumiditySensor, Oversample16);
    Serial.print(F("ON"));
    else
    BME280.setOversampling(HumiditySensor, SensorOff);
    Serial.print(F("OFF"));

    Serial.println(F(" humidity sensing"));
    Serial.print(F("- Each measurement cycle will now take "));
    Serial.print(BME280.measurementTime(MaximumMeasure) / 1000.0);
    Serial.println(F("ms.n"));


    Serial.println(F("condition after loop = "));
    //status_pin();
    Serial.println();



    float altitude(const float seaLevel = 1013.25)
    int32_t temp, hum, press;
    BME280.getSensorData(temp, hum, press);
    float Altitude = 44330.0 * (1.0 - pow(((float)press / 100.0) / seaLevel, 0.1903));
    return (Altitude);


    void printdata ()
    BME280.getSensorData(temperature, humidity, pressure);
    Serial.print(F("Temperature : "));
    Serial.print(temperature / 100.0);
    Serial.println(F(" *C"));
    if (BME280.getOversampling(HumiditySensor) != 0)
    Serial.print(F("Humidity : "));
    Serial.print(humidity / 100.0);
    Serial.println(F(" %"));

    Serial.print(F("Pressure : "));
    Serial.print(pressure / 100.0);
    Serial.println(F(" hPa"));
    Serial.print(F("Altitude : "));
    Serial.print(altitude());
    Serial.println(F(" m"));
    Serial.println();
    delay(5000);



    Serial output always says Unable to find BME280. Waiting 3 seconds. I know it's because CS pin cannot detect. I think something is missing my code.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a project for my graduate degree.



      It's about Monitoring Temperature and Humidity using Message Queuing Telemetry Transport on Serial NodeJS. To get sensor data to Wemos D1 Mini and NodeJS had done, also on my MQTT had done. My lecturer wants to get data from two sensors at the edges of the room, he suggests using a 74HC138. I'm using this 74HC138 Datasheet and I'm using this BME280 Library.



      This is my schematic:



      Schematic



      This is my code:



      #include <BME280.h>

      const uint32_t SERIAL_SPEED = 115200;
      const uint8_t SCK_PIN = D5;
      const uint8_t MISO_PIN = D6;
      const uint8_t MOSI_PIN = D7;
      const uint8_t SPI_CS1_PIN = 15;
      const uint8_t SPI_CS2_PIN = 14;
      const uint8_t GPIO[3] = D1, D2, D3;

      static int32_t temperature, humidity, pressure;
      int i;

      BME280_Class BME280;

      void setup()
      Serial.begin(SERIAL_SPEED);
      pinMode(SCK_PIN, OUTPUT);
      pinMode(MOSI_PIN, OUTPUT);
      pinMode(MISO_PIN, INPUT);

      SPI.begin();
      SPI.setBitOrder(LSBFIRST);

      Serial.println(F("condition before loop = "));
      //status_pin();
      for (i = 0; i < 3; i++)
      pinMode(GPIO[i], INPUT);
      digitalWrite(GPIO[i], HIGH);

      pinMode(SPI_CS1_PIN, OUTPUT);
      pinMode(SPI_CS2_PIN, OUTPUT);

      Serial.println(F("Starting Hardware SPIDemo example program for BME280"));
      Serial.print(F("- Initializing BME280 sensorn"));

      if ( (!BME280.begin(SPI_CS1_PIN) && (!BME280.begin(SPI_CS2_PIN) )
      Serial.println(F("- Unable to find BME280. Waiting 3 seconds."));
      delay(3000);


      BME280.mode(SleepMode);
      Serial.print(F("- Sensor detected in operating mode ""));
      Serial.print(BME280.mode());
      Serial.println(F(""."));
      if (BME280.mode() == 0)
      Serial.print(F("- Turning sensor to normal mode, mode is now ""));
      Serial.print(BME280.mode(NormalMode));
      Serial.println(""");

      Serial.println(F("- Setting 16x oversampling for all sensors"));
      BME280.setOversampling(TemperatureSensor, Oversample16);
      BME280.setOversampling(HumiditySensor, Oversample16);
      BME280.setOversampling(PressureSensor, Oversample16);
      Serial.println(F("- Setting IIR filter to maximum value of 16 samples"));
      BME280.iirFilter(IIR16);
      Serial.println(F("- Setting time between measurements to 1 second"));
      BME280.inactiveTime(inactive1000ms);
      Serial.print(F("- Each measurement cycle will take "));
      Serial.print(BME280.measurementTime(MaximumMeasure) / 1000);
      Serial.println(F("ms.nn"));


      void loop()
      static uint8_t loopCounter = 0;
      byte pin1 = 0x74;
      byte pin2 = 0x71;

      if ( BME280.begin(SPI_CS1_PIN) == 1 )
      selpin(pin1);
      Serial.println(F(" Data Bme = "));
      else if ( BME280.begin(SPI_CS2_PIN) == 1 )
      selpin(pin2);
      Serial.println(F(" Data Bme = "));
      printdata();


      if (++loopCounter % 10 == 0)
      Serial.print(F("n- Turning "));
      if (BME280.getOversampling(HumiditySensor) == 0)
      BME280.setOversampling(HumiditySensor, Oversample16);
      Serial.print(F("ON"));
      else
      BME280.setOversampling(HumiditySensor, SensorOff);
      Serial.print(F("OFF"));

      Serial.println(F(" humidity sensing"));
      Serial.print(F("- Each measurement cycle will now take "));
      Serial.print(BME280.measurementTime(MaximumMeasure) / 1000.0);
      Serial.println(F("ms.n"));


      Serial.println(F("condition after loop = "));
      //status_pin();
      Serial.println();



      float altitude(const float seaLevel = 1013.25)
      int32_t temp, hum, press;
      BME280.getSensorData(temp, hum, press);
      float Altitude = 44330.0 * (1.0 - pow(((float)press / 100.0) / seaLevel, 0.1903));
      return (Altitude);


      void printdata ()
      BME280.getSensorData(temperature, humidity, pressure);
      Serial.print(F("Temperature : "));
      Serial.print(temperature / 100.0);
      Serial.println(F(" *C"));
      if (BME280.getOversampling(HumiditySensor) != 0)
      Serial.print(F("Humidity : "));
      Serial.print(humidity / 100.0);
      Serial.println(F(" %"));

      Serial.print(F("Pressure : "));
      Serial.print(pressure / 100.0);
      Serial.println(F(" hPa"));
      Serial.print(F("Altitude : "));
      Serial.print(altitude());
      Serial.println(F(" m"));
      Serial.println();
      delay(5000);



      Serial output always says Unable to find BME280. Waiting 3 seconds. I know it's because CS pin cannot detect. I think something is missing my code.










      share|improve this question















      I have a project for my graduate degree.



      It's about Monitoring Temperature and Humidity using Message Queuing Telemetry Transport on Serial NodeJS. To get sensor data to Wemos D1 Mini and NodeJS had done, also on my MQTT had done. My lecturer wants to get data from two sensors at the edges of the room, he suggests using a 74HC138. I'm using this 74HC138 Datasheet and I'm using this BME280 Library.



      This is my schematic:



      Schematic



      This is my code:



      #include <BME280.h>

      const uint32_t SERIAL_SPEED = 115200;
      const uint8_t SCK_PIN = D5;
      const uint8_t MISO_PIN = D6;
      const uint8_t MOSI_PIN = D7;
      const uint8_t SPI_CS1_PIN = 15;
      const uint8_t SPI_CS2_PIN = 14;
      const uint8_t GPIO[3] = D1, D2, D3;

      static int32_t temperature, humidity, pressure;
      int i;

      BME280_Class BME280;

      void setup()
      Serial.begin(SERIAL_SPEED);
      pinMode(SCK_PIN, OUTPUT);
      pinMode(MOSI_PIN, OUTPUT);
      pinMode(MISO_PIN, INPUT);

      SPI.begin();
      SPI.setBitOrder(LSBFIRST);

      Serial.println(F("condition before loop = "));
      //status_pin();
      for (i = 0; i < 3; i++)
      pinMode(GPIO[i], INPUT);
      digitalWrite(GPIO[i], HIGH);

      pinMode(SPI_CS1_PIN, OUTPUT);
      pinMode(SPI_CS2_PIN, OUTPUT);

      Serial.println(F("Starting Hardware SPIDemo example program for BME280"));
      Serial.print(F("- Initializing BME280 sensorn"));

      if ( (!BME280.begin(SPI_CS1_PIN) && (!BME280.begin(SPI_CS2_PIN) )
      Serial.println(F("- Unable to find BME280. Waiting 3 seconds."));
      delay(3000);


      BME280.mode(SleepMode);
      Serial.print(F("- Sensor detected in operating mode ""));
      Serial.print(BME280.mode());
      Serial.println(F(""."));
      if (BME280.mode() == 0)
      Serial.print(F("- Turning sensor to normal mode, mode is now ""));
      Serial.print(BME280.mode(NormalMode));
      Serial.println(""");

      Serial.println(F("- Setting 16x oversampling for all sensors"));
      BME280.setOversampling(TemperatureSensor, Oversample16);
      BME280.setOversampling(HumiditySensor, Oversample16);
      BME280.setOversampling(PressureSensor, Oversample16);
      Serial.println(F("- Setting IIR filter to maximum value of 16 samples"));
      BME280.iirFilter(IIR16);
      Serial.println(F("- Setting time between measurements to 1 second"));
      BME280.inactiveTime(inactive1000ms);
      Serial.print(F("- Each measurement cycle will take "));
      Serial.print(BME280.measurementTime(MaximumMeasure) / 1000);
      Serial.println(F("ms.nn"));


      void loop()
      static uint8_t loopCounter = 0;
      byte pin1 = 0x74;
      byte pin2 = 0x71;

      if ( BME280.begin(SPI_CS1_PIN) == 1 )
      selpin(pin1);
      Serial.println(F(" Data Bme = "));
      else if ( BME280.begin(SPI_CS2_PIN) == 1 )
      selpin(pin2);
      Serial.println(F(" Data Bme = "));
      printdata();


      if (++loopCounter % 10 == 0)
      Serial.print(F("n- Turning "));
      if (BME280.getOversampling(HumiditySensor) == 0)
      BME280.setOversampling(HumiditySensor, Oversample16);
      Serial.print(F("ON"));
      else
      BME280.setOversampling(HumiditySensor, SensorOff);
      Serial.print(F("OFF"));

      Serial.println(F(" humidity sensing"));
      Serial.print(F("- Each measurement cycle will now take "));
      Serial.print(BME280.measurementTime(MaximumMeasure) / 1000.0);
      Serial.println(F("ms.n"));


      Serial.println(F("condition after loop = "));
      //status_pin();
      Serial.println();



      float altitude(const float seaLevel = 1013.25)
      int32_t temp, hum, press;
      BME280.getSensorData(temp, hum, press);
      float Altitude = 44330.0 * (1.0 - pow(((float)press / 100.0) / seaLevel, 0.1903));
      return (Altitude);


      void printdata ()
      BME280.getSensorData(temperature, humidity, pressure);
      Serial.print(F("Temperature : "));
      Serial.print(temperature / 100.0);
      Serial.println(F(" *C"));
      if (BME280.getOversampling(HumiditySensor) != 0)
      Serial.print(F("Humidity : "));
      Serial.print(humidity / 100.0);
      Serial.println(F(" %"));

      Serial.print(F("Pressure : "));
      Serial.print(pressure / 100.0);
      Serial.println(F(" hPa"));
      Serial.print(F("Altitude : "));
      Serial.print(altitude());
      Serial.println(F(" m"));
      Serial.println();
      delay(5000);



      Serial output always says Unable to find BME280. Waiting 3 seconds. I know it's because CS pin cannot detect. I think something is missing my code.







      esp8266 sensors temperature-sensor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 8 at 15:10









      Greenonline

      1,90741539




      1,90741539










      asked Sep 8 at 9:55









      abu-ahmed al-khatiri

      1288




      1288




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Using a 74HC138 in this situation is both pointless and wasteful. You're using 3 pins (or 4 - see below) to drive a chip to select between 2 pins. That's wasting one more pin than you would by directly driving the CS pins.



          The only time it would make sense would be if you were wanting to connect with 5 or more devices, in which case you would get a saving in the number of pins used.



          Anyway - what you are missing from your code is absolutely everything to do with the 74HC138. First off, you're using a library which assumes the CS pins are directly connected to the MCU. Second, there is no code to set the A/B/C pins to the right state to select an output.



          If you really must use the 74HC138 I would suggest:



          • Use the same CS pin in your code for every BMS280 instance.

          • Connect that CS pin to G2A on the 74HC138

          • Use the following procedure to access a sensor:

            1. Select the correct output on the 74HC138 by setting the A/B/C pins to the binary representation of the output you want

            2. Perform the BME280 operations you want on the relevant instance


          schematic





          simulate this circuit – Schematic created using CircuitLab



          The CS pin you passed to the BME280 instances will control the state of the currently selected output on the 74HC138, turning it into a demultiplexer controlled by the A/B/C pins. Using multiple instances of the BME280 class associated with each selected output will allow proper tracking of any internal state and data associated with the device connected to that output.



          Or, and this is just throwing ideas out there, don't use the 74HC138 and tell your "lecturer" that it's a stupid idea to use 3 pins (4 including the chip select pin) to control a pair of devices that need a total of 2 pins.






          share|improve this answer






















          • i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
            – abu-ahmed al-khatiri
            Sep 8 at 11:17










          • Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
            – Majenko♦
            Sep 8 at 12:07










          • yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
            – abu-ahmed al-khatiri
            Sep 8 at 12:20










          • Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
            – Majenko♦
            Sep 8 at 13:11










          • ok thanks sir, i trying.
            – abu-ahmed al-khatiri
            Sep 8 at 13:13










          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("schematics", function ()
          StackExchange.schematics.init();
          );
          , "cicuitlab");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "540"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f55938%2fhow-do-i-select-sensor-using-chip-74hc138-on-wemos-d1-mini%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          Using a 74HC138 in this situation is both pointless and wasteful. You're using 3 pins (or 4 - see below) to drive a chip to select between 2 pins. That's wasting one more pin than you would by directly driving the CS pins.



          The only time it would make sense would be if you were wanting to connect with 5 or more devices, in which case you would get a saving in the number of pins used.



          Anyway - what you are missing from your code is absolutely everything to do with the 74HC138. First off, you're using a library which assumes the CS pins are directly connected to the MCU. Second, there is no code to set the A/B/C pins to the right state to select an output.



          If you really must use the 74HC138 I would suggest:



          • Use the same CS pin in your code for every BMS280 instance.

          • Connect that CS pin to G2A on the 74HC138

          • Use the following procedure to access a sensor:

            1. Select the correct output on the 74HC138 by setting the A/B/C pins to the binary representation of the output you want

            2. Perform the BME280 operations you want on the relevant instance


          schematic





          simulate this circuit – Schematic created using CircuitLab



          The CS pin you passed to the BME280 instances will control the state of the currently selected output on the 74HC138, turning it into a demultiplexer controlled by the A/B/C pins. Using multiple instances of the BME280 class associated with each selected output will allow proper tracking of any internal state and data associated with the device connected to that output.



          Or, and this is just throwing ideas out there, don't use the 74HC138 and tell your "lecturer" that it's a stupid idea to use 3 pins (4 including the chip select pin) to control a pair of devices that need a total of 2 pins.






          share|improve this answer






















          • i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
            – abu-ahmed al-khatiri
            Sep 8 at 11:17










          • Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
            – Majenko♦
            Sep 8 at 12:07










          • yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
            – abu-ahmed al-khatiri
            Sep 8 at 12:20










          • Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
            – Majenko♦
            Sep 8 at 13:11










          • ok thanks sir, i trying.
            – abu-ahmed al-khatiri
            Sep 8 at 13:13














          up vote
          2
          down vote













          Using a 74HC138 in this situation is both pointless and wasteful. You're using 3 pins (or 4 - see below) to drive a chip to select between 2 pins. That's wasting one more pin than you would by directly driving the CS pins.



          The only time it would make sense would be if you were wanting to connect with 5 or more devices, in which case you would get a saving in the number of pins used.



          Anyway - what you are missing from your code is absolutely everything to do with the 74HC138. First off, you're using a library which assumes the CS pins are directly connected to the MCU. Second, there is no code to set the A/B/C pins to the right state to select an output.



          If you really must use the 74HC138 I would suggest:



          • Use the same CS pin in your code for every BMS280 instance.

          • Connect that CS pin to G2A on the 74HC138

          • Use the following procedure to access a sensor:

            1. Select the correct output on the 74HC138 by setting the A/B/C pins to the binary representation of the output you want

            2. Perform the BME280 operations you want on the relevant instance


          schematic





          simulate this circuit – Schematic created using CircuitLab



          The CS pin you passed to the BME280 instances will control the state of the currently selected output on the 74HC138, turning it into a demultiplexer controlled by the A/B/C pins. Using multiple instances of the BME280 class associated with each selected output will allow proper tracking of any internal state and data associated with the device connected to that output.



          Or, and this is just throwing ideas out there, don't use the 74HC138 and tell your "lecturer" that it's a stupid idea to use 3 pins (4 including the chip select pin) to control a pair of devices that need a total of 2 pins.






          share|improve this answer






















          • i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
            – abu-ahmed al-khatiri
            Sep 8 at 11:17










          • Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
            – Majenko♦
            Sep 8 at 12:07










          • yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
            – abu-ahmed al-khatiri
            Sep 8 at 12:20










          • Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
            – Majenko♦
            Sep 8 at 13:11










          • ok thanks sir, i trying.
            – abu-ahmed al-khatiri
            Sep 8 at 13:13












          up vote
          2
          down vote










          up vote
          2
          down vote









          Using a 74HC138 in this situation is both pointless and wasteful. You're using 3 pins (or 4 - see below) to drive a chip to select between 2 pins. That's wasting one more pin than you would by directly driving the CS pins.



          The only time it would make sense would be if you were wanting to connect with 5 or more devices, in which case you would get a saving in the number of pins used.



          Anyway - what you are missing from your code is absolutely everything to do with the 74HC138. First off, you're using a library which assumes the CS pins are directly connected to the MCU. Second, there is no code to set the A/B/C pins to the right state to select an output.



          If you really must use the 74HC138 I would suggest:



          • Use the same CS pin in your code for every BMS280 instance.

          • Connect that CS pin to G2A on the 74HC138

          • Use the following procedure to access a sensor:

            1. Select the correct output on the 74HC138 by setting the A/B/C pins to the binary representation of the output you want

            2. Perform the BME280 operations you want on the relevant instance


          schematic





          simulate this circuit – Schematic created using CircuitLab



          The CS pin you passed to the BME280 instances will control the state of the currently selected output on the 74HC138, turning it into a demultiplexer controlled by the A/B/C pins. Using multiple instances of the BME280 class associated with each selected output will allow proper tracking of any internal state and data associated with the device connected to that output.



          Or, and this is just throwing ideas out there, don't use the 74HC138 and tell your "lecturer" that it's a stupid idea to use 3 pins (4 including the chip select pin) to control a pair of devices that need a total of 2 pins.






          share|improve this answer














          Using a 74HC138 in this situation is both pointless and wasteful. You're using 3 pins (or 4 - see below) to drive a chip to select between 2 pins. That's wasting one more pin than you would by directly driving the CS pins.



          The only time it would make sense would be if you were wanting to connect with 5 or more devices, in which case you would get a saving in the number of pins used.



          Anyway - what you are missing from your code is absolutely everything to do with the 74HC138. First off, you're using a library which assumes the CS pins are directly connected to the MCU. Second, there is no code to set the A/B/C pins to the right state to select an output.



          If you really must use the 74HC138 I would suggest:



          • Use the same CS pin in your code for every BMS280 instance.

          • Connect that CS pin to G2A on the 74HC138

          • Use the following procedure to access a sensor:

            1. Select the correct output on the 74HC138 by setting the A/B/C pins to the binary representation of the output you want

            2. Perform the BME280 operations you want on the relevant instance


          schematic





          simulate this circuit – Schematic created using CircuitLab



          The CS pin you passed to the BME280 instances will control the state of the currently selected output on the 74HC138, turning it into a demultiplexer controlled by the A/B/C pins. Using multiple instances of the BME280 class associated with each selected output will allow proper tracking of any internal state and data associated with the device connected to that output.



          Or, and this is just throwing ideas out there, don't use the 74HC138 and tell your "lecturer" that it's a stupid idea to use 3 pins (4 including the chip select pin) to control a pair of devices that need a total of 2 pins.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 8 at 12:11

























          answered Sep 8 at 10:56









          Majenko♦

          62.2k42773




          62.2k42773











          • i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
            – abu-ahmed al-khatiri
            Sep 8 at 11:17










          • Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
            – Majenko♦
            Sep 8 at 12:07










          • yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
            – abu-ahmed al-khatiri
            Sep 8 at 12:20










          • Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
            – Majenko♦
            Sep 8 at 13:11










          • ok thanks sir, i trying.
            – abu-ahmed al-khatiri
            Sep 8 at 13:13
















          • i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
            – abu-ahmed al-khatiri
            Sep 8 at 11:17










          • Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
            – Majenko♦
            Sep 8 at 12:07










          • yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
            – abu-ahmed al-khatiri
            Sep 8 at 12:20










          • Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
            – Majenko♦
            Sep 8 at 13:11










          • ok thanks sir, i trying.
            – abu-ahmed al-khatiri
            Sep 8 at 13:13















          i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
          – abu-ahmed al-khatiri
          Sep 8 at 11:17




          i'm still little confuse sir, your mean "Connect that CS pin to Q2A on the 74HC138" i must connect CS to enable input pin and where i must connect Y1-Y2 output pin? where i can find example of send the binary representation to chip. i really noob about chip gate sir
          – abu-ahmed al-khatiri
          Sep 8 at 11:17












          Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
          – Majenko♦
          Sep 8 at 12:07




          Sorry, that's a G on your little picture, not a Q. Looks the same to me. You don't "send" the binary representation to the chip. You set your three pins connected to A/B/C to that binary representation. Output 0 is LOW/LOW/LOW. Output 1 is HIGH/LOW/LOW. Output 2 is LOW/HIGH/LOW, Output 3 is HIGH/HIGH/LOW, etc. Outputs go as they are now.
          – Majenko♦
          Sep 8 at 12:07












          yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
          – abu-ahmed al-khatiri
          Sep 8 at 12:20




          yes that's G, it's fine, ok i can see what do you mean on your picture, to set A/B/C pin what i must using pinMode or digitalWrite sir?
          – abu-ahmed al-khatiri
          Sep 8 at 12:20












          Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
          – Majenko♦
          Sep 8 at 13:11




          Both. pinMode to set them as outputs, and digitalWrite to set the logic levels.
          – Majenko♦
          Sep 8 at 13:11












          ok thanks sir, i trying.
          – abu-ahmed al-khatiri
          Sep 8 at 13:13




          ok thanks sir, i trying.
          – abu-ahmed al-khatiri
          Sep 8 at 13:13

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f55938%2fhow-do-i-select-sensor-using-chip-74hc138-on-wemos-d1-mini%23new-answer', 'question_page');

          );

          Post as a guest













































































          這個網誌中的熱門文章

          How to combine Bézier curves to a surface?

          Carbon dioxide

          Why am i infinitely getting the same tweet with the Twitter Search API?