0
\$\begingroup\$

I am trying to read two gas sensors (MQ4 for methane and MQ7 for carbon monoxide) with my NodeMCU ESP8266. Both sensors require 5v (for the heaters) and have 4 pins (GND, VCC, DO, AO). Digital outputs are HIGH when a threshold is exceeded (adjustable on the breakout board). In order to read analog outputs, I am using MCP3008 10 bit ADC, which works with higher accuracy at 5V. ESP8266 is however 3.3V.

I am using 8-channel bi-directional level shifter to communicate with MCP3008. MCP3008 and sensors are connected to Vin and GND on NodeMCU ESP8266 board, which provides 5V.

Level shifter is also connected to 5V on HV side and 3.3V on LV side. Level shifter works fine for digital outputs of sensors. But when it comes to SPI between MCP3008 and NodeMCU ESP8266, I always read high values (1023). When I get rid of the level shifter and power MCP3008 with 3.3V with a potentiometer, I can read values between 0-1023. Also the sensors output analog values between 0-5V (tested with lighter gas and multimeter). So there must be a problem between level shifter and SPI communication.

On the software side, I tried to do the communication manually, with SPI.h and Adafruit_MCP3008.h libraries. They work fine, when I remove level shifter and operate MCP3008 with 3.3V.

I was wondering, what the problem might be? I don't have an oscillator and it is my first "real" electronics project. Excuse my lack of jargon... Thanks!

EDIT: I use this level shifter: https://www.amazon.de/gp/product/B01FRQUQCS/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 Schematic Level shifter

\$\endgroup\$
  • \$\begingroup\$ Which level shifter are you using (I do not see it mentioned in your question)? Schematics would be nice to add too. \$\endgroup\$ – Martin Feb 18 '18 at 20:32
  • \$\begingroup\$ @Martin I added the information. \$\endgroup\$ – Genom Feb 18 '18 at 20:40
  • \$\begingroup\$ Do you have access to a scope you can probe it with? If not, what do you read if you stop driving the chip select pin, and instead experiment with grounding the MISO line on each of the low voltage and high voltage sides? \$\endgroup\$ – Chris Stratton Feb 18 '18 at 21:00
  • \$\begingroup\$ How fast are you trying to run the SPI? Have you tried slowing it down to see if the level translation is messing up the SPI timing? Can you look at the waveforms during the SPI transfers with an oscilloscope? \$\endgroup\$ – crj11 Feb 18 '18 at 21:03
  • \$\begingroup\$ Incidentally, while not part of your main experiment "power MCP3008 with 3.3V with a potentiometer" is an extremely unsound idea. You can't use a potentiometer to regulate voltage for an active circuit with a power draw that varies by phase of operation. You would need a high bandwidth control loop to continuously adjust the potentiometer to account for the changes in load - an assembly traditional sold under the name "linear voltage regulator". \$\endgroup\$ – Chris Stratton Feb 18 '18 at 21:05
0
\$\begingroup\$

My guess is that the problem is caused by the edges of the outgoing clock getting messed up by the level translator. That translator is really designed for I2C signals, which have very slow edges. SPI signals are designed to be driven high and low, while I2C signals are driven low and pulled high by resistors. You might be able to get it working by changing the HV side pullups to a lower value.

TI makes the SN74GTL2003 which is similar to the NMOS level translator you are using, but is designed to work at high speed. The main difference is that the TI part has several matched MOSFETs and uses one of them that's not used as an I/O to set the gate voltage for all of the MOSFETs. This sets the maximum voltage that is passed through. Pull up resistors on the high side then set the maximum voltage on the high side. This is all explained in the TI documentation.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.