Analog Pins

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Analog Pins

Post by sprinkfitter » Wed Sep 27, 2017 1:51 pm

What is the best way to increase the number of analog pins on an esp8266. I need 4 more Analog pins for sensors. Thank You in advance :?: :?: :?:


sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Analog Pins

Post by sprinkfitter » Wed Sep 27, 2017 6:24 pm

Can multiplexer like the 74hc4051 be used?

User avatar
deshipu
Posts: 1347
Joined: Thu May 28, 2015 5:54 pm

Re: Analog Pins

Post by deshipu » Wed Sep 27, 2017 7:19 pm

And if you are using a Wemos D1 Mini, by total coincidence I just started selling my homebrew analog shield: https://www.tindie.com/products/deshipu ... r-d1-mini/

sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Analog Pins

Post by sprinkfitter » Wed Sep 27, 2017 11:02 pm

I have a D1 not the mini. But i might be getting one to try out. Your board looks solid.

User avatar
mcauser
Posts: 395
Joined: Mon Jun 15, 2015 8:03 am

Re: Analog Pins

Post by mcauser » Thu Sep 28, 2017 12:05 am

Judging by this page, yes you can use a 74hc4051 multiplexer:
https://internetofhomethings.com/homethings/?p=530

sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Analog Pins

Post by sprinkfitter » Thu Sep 28, 2017 12:50 am

Thats an Arduino hook up. I wonder if someone has hook it up with Micropython???

crizeo
Posts: 42
Joined: Sun Aug 06, 2017 12:55 pm
Location: Germany

Re: Analog Pins

Post by crizeo » Thu Sep 28, 2017 12:59 am

I am using the MCP3008, which works perfectly for up to 8 analog inputs using the SPI pins (~2$). You could also use the MCP3004, I think, but it only has 4 analog inputs. I wrote a simple driver (mcp3008.py):

Code: Select all

from machine import Pin

class MCP3008:
    def __init__(self, clk=14, mosi=13, miso=12, cs=15):
        self._clk = Pin(clk, Pin.OUT)
        self._mosi = Pin(mosi, Pin.OUT)
        self._miso = Pin(miso, Pin.IN)
        self._cs = Pin(cs, Pin.OUT)

    def read(self, channel):
        # """ Reads an analog value from the given channel (0-7) and returns it. """
        if channel not in range(0, 8):
            raise ValueError("channel must be 0-7")

        self._cs(1)  # negative edge
        self._cs(0)
        self._clk(0)

        send_cmd = channel
        send_cmd |= 0b00011000  # 0x18 (start bit + single/ended)

        # send bits (only 5 bits considered)
        for i in range(5):
            self._mosi(bool(send_cmd & 0x10))  # check bit on index 4
            self._clk(1)  # negative edge
            self._clk(0)
            send_cmd <<= 1

        # receive value from MCP
        v = 0
        for i in range(11):
            self._clk(1)  # negative edge
            self._clk(0)
            v <<= 1
            if self._miso():
                v |= 0x01

        return v
The layout of the MCP3008 can be found on the internet (e. g. here).
You will have to make the follwing connections (MCP3004/8 <-> ESP8266):
  • V_dd <-> 3.3V
  • V_ref <-> 3.3V
  • AGND <-> GND
  • CLK <-> SCK (14)
  • D_out <-> MISO (12)
  • D_in <-> MOSI (13)
  • CS <-> 15
  • DGND <-> GND
Then you can read values from the different channels that can be addressed using integers 0 to 7. Example:

Code: Select all

from mcp3008 import MCP3008
mcp = MCP3008()
print("Value on CH0:", mcp.read(0))
You can change the pins in the constructor if you like (e. g. CS pin can be any pin you like). If you need more than 8 extra ADC inputs, you can probably (not tested) simply connect multiple MCP3008 (on the same three SPI pins), but use a different CS pin for each MCP.

sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Analog Pins

Post by sprinkfitter » Thu Sep 28, 2017 5:13 pm

Thank You for all the info. That helps out a-lot. Great Forum!!!! Thanks again

sprinkfitter
Posts: 32
Joined: Wed Sep 27, 2017 1:42 pm
Location: Louisville Ky

Re: Analog Pins

Post by sprinkfitter » Thu Oct 05, 2017 5:11 pm

:D :D :D :D :D Now that work with the MCP3008. :arrow: :arrow: :arrow: :arrow: Thank You

Post Reply