
make the circuit diagram according to this Component Connected To ESP32 PinMultiplexer SIGA0 (Analog pin)MUX S0D0MUX S1D5MUX S2D6MUX S3D8MQ-2MUX Channel 0MQ-135MUX Channel 1LDRMUX Channel 2Sharp Dust Sensor OutputA3 (or D34)Sharp LED ControlD6 (you used it earlier)DHT11D7OLED I2C SDAD21OLED I2C SCLD22Power linesVIN → 5V, GND → GND +-------------------+ +----------------+ +-----------------+ | | | | | | | MQ135 Sensor |----->| | | | | (CO₂, VOCs) | | | | | +-------------------+ | | | | | | | | +-------------------+ | | | | | | | | | | | MQ2 Sensor |----->| ESP32 |----->| OLED Display | | (Smoke, LPG) | | (Microcontroller) | (128x64 I2C) | +-------------------+ | | | | | | | | +-------------------+ | | | | | |----->| | | | | DHT11 Sensor | | | | | | (Temp, Humidity) | +----------------+ +-----------------+ +-------------------+ # ESP32 Air Quality Monitor — Circuit Diagram & Detailed Wiring **Project:** ESP32 Air Quality Monitor (MQ-2, MQ-135, Sharp dust, DHT11, LDR, OLED, 16‑ch MUX) **Goal:** Clear, school-fair–ready circuit diagram and step‑by‑step wiring instructions so you can build the circuit reliably. --- ## 1) Summary of connections (as you requested) | Component | Connected to ESP32 pin (D-label) | | ----------------------------------- | ------------------------------------------------------: | | Multiplexer SIG | A0 (Analog input) | | MUX S0 | D0 | | MUX S1 | D5 | | MUX S2 | D6 | | MUX S3 | D8 | | MQ-2 | MUX channel 0 (SIG via channel 0) | | MQ-135 | MUX channel 1 | | LDR | MUX channel 2 | | Sharp Dust Sensor analog output | A3 (or D34) | | Sharp LED control (dust sensor LED) | **DO NOT USE D6** — see note below; use **D14** instead | | DHT11 data | D7 | | OLED SDA | D21 | | OLED SCL | D22 | | Power | VIN → 5V, GND → GND | > **Important note:** You listed `MUX S2 -> D6` and also `Sharp LED control -> D6` which is a pin conflict. I have moved the Sharp LED control to **D14** in the detailed wiring so the MUX select lines are exclusive. If you want a different pin for the LED, you can change it later but **do not** leave both on the same pin. --- ## 2) Components & extra parts you will need * ESP32 development board (DevKitC or similar) * CD74HC4067 or 74HC4067 (16-channel analog multiplexer) **or** 4051 (8-channel) — wiring below assumes a 16‑channel device with S0..S3 * MQ-2 gas sensor module (module with analog output) * MQ-135 gas sensor module (module with analog output) * Sharp optical dust sensor (GP2Y1010AU0F or similar) * DHT11 temperature & humidity sensor * LDR (photoresistor) + 10k resistor for divider * 2 × resistors for analog voltage dividers (10k + 20k recommended) for each 5V sensor analog output (see section 5) * Jumper wires, breadboard, small perf board (optional) * 0.1 µF decoupling caps (helpful) and a 10 µF capacitor for MQ modules (optional) * USB cable and 5V supply / power bank (or power from VIN) --- ## 3) High-level Block Diagram (text/ASCII) ``` [MQ-135] --+ [MQ-2 ] --+--> [MUX channel inputs] -- SIG --> A0 (ESP32) [LDR ] --+ [Sharp Dust Sensor] --> A3 (D34 on ESP32) Sharp LED control --> D14 (ESP32) [DHT11] --> D7 (ESP32) [OLED 128x64 I2C] --> SDA:D21 SCL:D22 [MUX S0..S3] --> D0, D5, D6, D8 (ESP32) Power: VIN->5V, GND->GND (common ground) (All analog outputs that can reach 5V must be scaled to 0-3.3V before entering ESP32 ADC pins.) ``` --- ## 4) Multiplexer wiring details Wire the multiplexer as follows (example using CD74HC4067): * VCC -> 5V (power the sensor side at 5V if modules need 5V) * GND -> GND (common ground with ESP32) * SIG (COM) -> **A0** (ESP32 analog pin) * S0 -> D0 (ESP32) * S1 -> D5 (ESP32) * S2 -> D6 (ESP32) * S3 -> D8 (ESP32) * INH (enable/inhibit) -> GND (active low; tie low to enable) **Channel mapping (example):** * Channel 0 (S3..S0 = 0000) -> MQ-2 analog output * Channel 1 (0001) -> MQ-135 analog output * Channel 2 (0010) -> LDR analog divider output * (Reserve other channels for future sensors) Use the `selectMuxChannel(int channel)` function in your code to set S0..S3. --- ## 5) Protecting ESP32 ADC from 5V outputs (voltage divider) Many gas sensor modules and the Sharp dust analog output can produce voltages close to 5V. ESP32 ADC input max is ~3.3V. Use a simple voltage divider for **each** analog sensor line that may reach 5V. **Suggested divider (maps 0-5V to ≈0-3.33V):** * Rtop = 10 kΩ * Rbottom = 20 kΩ Connection: ``` Sensor analog out ---- Rtop (10k) ----+----> ESP32 ADC pin | Rbottom (20k) | GND ``` This gives Vout = Vin * (20/(10+20)) = Vin * 0.666 → 5V -> 3.33V safe. Use this divider on: * MQ-2 analog out (before the MUX input) * MQ-135 analog out * Sharp dust analog out (if it can reach 5V) > If your sensor modules are already 3.3V modules, you can skip the divider. Always measure with a multimeter first. --- ## 6) Sharp dust sensor special timing (LED pulse) For Sharp GP2Y1010AU0F: * Use the LED control pin from ESP32 (we assigned **D14**) to pulse the sensor LED. * Typical timing (from datasheet): 1. Drive LED LOW (or HIGH depending on wiring) — follow your sensor’s module convention. In your existing code you used `digitalWrite(LED, LOW); delayMicroseconds(280); readAnalog(); delayMicroseconds(40); digitalWrite(LED, HIGH);` — this approach is OK if verified against your module. 2. Read analog value on A3 (D34) after the 280 µs delay. ***Important:*** Verify whether your sensor module expects active LOW or HIGH to turn on the internal IR LED. Test once with the datasheet and a brief code test. --- ## 7) DHT11 wiring * VCC -> 3.3V or 5V (module supports both; if powering other sensors at 5V, you may use 5V) * GND -> GND * Data -> D7 (ESP32) * If your DHT module has a pull-up resistor installed, you don’t need to add one. If not, add a 5k–10k pull-up between Data and VCC. --- ## 8) OLED wiring (I2C) * VCC -> 3.3V (recommended for OLED I2C modules) or 5V per module docs * GND -> GND * SDA -> D21 * SCL -> D22 If the OLED is 5V-tolerant and you power it from 5V, ensure the SDA/SCL voltage levels are safe; otherwise power it from 3.3V and share ground. --- ## 9) Full wiring table (final) | From | To | Notes | | --------------------- | --------------------------------------------------------------------- | ---------------------------------------------------------------------- | | MQ-2 analog out | MUX input pin for channel 0 (module side) — then SIG → A0 via divider | Use 10k/20k divider on the line feeding MUX if module outputs up to 5V | | MQ-135 analog out | MUX channel 1 (same divider) | | | LDR (voltage divider) | MUX channel 2 (read via SIG → A0) | LDR + 10k to form divider; output ~0-3.3V safe | | MUX SIG (COM) | A0 (ESP32) | ADC input reads selected channel | | MUX S0 | D0 | | | MUX S1 | D5 | | | MUX S2 | D6 | | | MUX S3 | D8 | | | Sharp dust analog | A3 (D34) via divider if needed | | | Sharp LED control | D14 (ESP32) | Pulse according to datasheet timing | | DHT11 data | D7 | | | OLED SDA | D21 | | | OLED SCL | D22 | | | VCC for sensors & mux | VIN → 5V supply (or separate 5V regulator) | Common GND required | | ESP32 GND | GND | Common ground for all modules | --- ## 10) Step-by-step breadboard build instructions 1. **Place ESP32** on the breadboard so pins are accessible. 2. **Place the multiplexer** (CD74HC4067) on the breadboard. 3. **Wire power rails**: connect 5V supply to VIN rail, connect ESP32 GND to ground rail. 4. **Connect MUX VCC/GND**: VCC→5V, GND→GND, INH→GND. 5. **Connect MUX SIG to A0** on ESP32 (SIG = COM pin). 6. **Wire MUX select pins**: S0→D0, S1→D5, S2→D6, S3→D8. 7. **Connect MQ-2**: module VCC→5V, GND→GND, Analog out→MUX channel 0 input pin (e.g., pin 0 on MUX). Add voltage divider between module analog out and MUX input if needed. 8. **Connect MQ-135** similarly to MUX channel 1. 9. **Connect LDR**: build an LDR divider (LDR + 10k) and connect its output to MUX channel 2. 10. **Connect Sharp Dust Sensor**: AO → A3 (D34) through divider; LED control → D14. 11. **Connect DHT11**: Data → D7, VCC→3.3V/5V, GND→GND. Add pull-up if needed. 12. **Connect OLED**: SDA→D21, SCL→D22, VCC→3.3V, GND→GND. 13. **Double-check all connections**, ensure no pins are shorted. 14. **Power up** the 5V supply and connect USB to ESP32. --- ## 11) Example MUX select function (code snippet) ```cpp void selectMuxChannel(int channel) { digitalWrite(MUX_S0, channel & 1); digitalWrite(MUX_S1, (channel >> 1) & 1); digitalWrite(MUX_S2, (channel >> 2) & 1); digitalWrite(MUX_S3, (channel >> 3) & 1); delayMicroseconds(5); // let signals settle } ``` Call `selectMuxChannel(CH_MQ2)` then `analogRead(MUX_SIG_PIN)` to read that sensor. --- ## 12) Calibration & Testing tips * **MQ sensors**: require a warm-up time (several minutes) and calibration in clean air. Use known concentrations if possible to map raw ADC to ppm. * **Sharp dust**: map measured voltage to dust concentration using datasheet equation or module guide. Use the pulse-timing carefully. * **LDR**: test in bright and dark light and record ADC ranges; you can normalize values in code. * **ADC nonlinearities**: ESP32 ADC is non-linear and noisy. Use averaging (sample many times and average) and optionally apply calibration curves. --- ## 13) Safety & practical notes * Do **not** feed >3.3V to ESP32 ADC pins. Use voltage dividers or level-shifting. * Use a **common ground** for all modules and ESP32. * Keep MQ modules ventilated during testing; they have heating elements. * Use proper power supply rated for the current (MQ heaters draw current). --- ## 14) Troubleshooting checklist * If a sensor reads 0 or constant: check wiring, confirm the MUX channel selection, measure the sensor analog out with multimeter. * If readings are very noisy: sample multiple times and average, add small RC filters (0.1uF + resistor). * If OLED shows garbage: check I2C address and wiring (SDA/SCL swapped?) and ensure correct voltage. * If the ESP32 resets: check power supply current and VIN wiring. --- ## 15) What I couldn't draw here I did a full textual electrical diagram and wiring table. If you want an actual image-style schematic (PNG/PDF) that you can print on chart paper or include in your science fair poster, tell me and I will generate a printable PDF schematic showing all wires laid out on a breadboard (I can produce a downloadable PDF next). --- If you want the **exact printable schematic** (visual image or PDF), reply: **"Make printable schematic PDF"** and I will produce a downloadable file with the breadboard picture and clear labelled wires.