Analog Pins

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
Posts: 147
Joined: Tue Oct 17, 2017 5:18 am

Re: Analog Pins

Post by liudr » Wed Oct 18, 2017 5:13 pm

mcauser wrote:
Thu Sep 28, 2017 12:05 am
Judging by this page, yes you can use a 74hc4051 multiplexer:
Cool! I like the purple board (and what it represents)! Nicely assembled. Did you hand solder your board?
Price is good too.

Odd. I swear I quoted a different reply. It's about the 74HC4051 breakout board.

User avatar
Posts: 3
Joined: Tue Jul 30, 2019 8:00 pm

Re: Analog Pins

Post by peaktech99 » Tue Jul 30, 2019 8:20 pm

Dear crizeo, dear forum, this is my very first post here..

After days of research and endless attempts of "trial & error" I finally decided to ask for your help. I would like to adapt crizeos code / class to make it work with the MCP3002 ADC which is a variant of the MCP3008 with only two input channels. Somehow, I cannot get the code to work properly, as always measures values far beyond 1023, e.g. 2036 or 2044 while the input as well as Vref is attached to 3.3V.

Can anyone help me out with what to change here? As I am new to uPython and SPI I cannot figure out what could be the cause for these strange values. I found a few other examples for the MCP3002/3004/3008, written in "regular" python, using the function spi.write_readinto(...). I think, to address CH_0 at LSB first it should be 0b11010000. But unfortunately I couldn't find a solution, as I am not sure how to manipulate (shift) the bits replied by the MCP3002. :oops: :?:

Thank you very much in advance!

User avatar
Posts: 3
Joined: Tue Jul 30, 2019 8:00 pm

Re: Analog Pins

Post by peaktech99 » Thu Aug 01, 2019 7:31 pm

Meanwhile, I think I found the right combination of bits and bytes to obtain correct values from the MCP3002. Any hints are highly welcome, though! As I'm still new to programming, this is a very first approach. I am sorry not to be able to supply a class yet.

Code: Select all

from machine import SPI, Pin
spi = SPI(1, baudrate=1000000, polarity=0, phase=0)

cs = Pin(15, Pin.OUT)

# Control & Data Registers:
# See datasheet for more information
# send 8 bit control :
# X, Strt, SGL|!DIFF, ODD|!SIGN, MSBF, X, X, X
# 0, 1,    1=SGL,     0 = CH0  , 0   , 0, 0, 0 = 96d

buffer = bytearray([96,0])
print("MOSI - first Byte:",bin(buffer[0]))
print("MOSI - second Byte:",bin(buffer[1]),"\n")

read = bytearray(2) 

# receive 10 bit data :
# receive data range: 000..3FF (10 bits)
# MSB first: (set control bit in cmd for LSB first)
# spidata[0] =  X,  X,  X,  X,  X,  0, B9, B8
# spidata[1] = B7, B6, B5, B4, B3, B2, B1, B0
# LSB: mask all but B9 & B8, shift to left and add to the MSB

print("MISO / raw - first Byte: ",bin(read[0]))
print("MISO / altered - first Byte: ",bin((read[0] & 3) << 8))
print("MISO - second Byte, OK as it is: ",bin(read[1]))
print("MISO / altered - all together: ",bin(((read[0] & 3) << 8) + read[1]))

value = (((read[0] & 3) << 8) + read[1])
print("ADC-value: ",value)
print("measured Voltage: ",((3.305/1023.0)*value),"V")

Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Analog Pins

Post by sspaman » Fri Sep 27, 2019 12:21 am

Did you ever get this working?

I have a bit banger for the MCP3008 I found on github I can give you if you want to take a look. I am trying to adapt the hardware spi since it seems like nonsense to use a library when the functions are already in uPython.

I'd be interested in your solution if you got it working.


User avatar
Posts: 3
Joined: Tue Jul 30, 2019 8:00 pm

Re: Analog Pins

Post by peaktech99 » Fri Sep 27, 2019 6:52 pm

Hi Chris,

I really had to "comb" several books, mainly by the help of, to find an explanation that suited my level of knowledge for those so-called "bitwise operations". One that has been helpful was the following: Also, Paulv's Blog entry has been of great help for me! Please have a look at ... g-and.html Here's one more link with a helpful illustration of the "steps" of a transmission by SPI.

As for the code I posted, I can say the results are close to the measurements of my multimeter. So I would assume, it is "working"? :? :)
Please feel free to run and test it on your ESP8266. You may have to adjust the voltage value of your power supply in the very last line when the measured voltage is calculated from the value returned by the MCP.

I hope you will manage to get going with my humble attempts of explanation.

User avatar
Posts: 41
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: Analog Pins

Post by ghayne » Sat Sep 28, 2019 9:51 am

deshipu wrote:
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: ... r-d1-mini/
That is a lovely looking piece of kit!

Posts: 16
Joined: Fri Nov 02, 2018 5:03 pm

Re: Analog Pins

Post by sspaman » Sat Sep 28, 2019 3:07 pm

I have it running now. I am running it in series with the MCP3008 library from Crizeo. Crozio's code is spot on and works great. However, the hardware SPI code you posted seems to work but the first byte returned from read[0] never changes despite the voltage on the pin.

So there is some small problem with the read and write on the SPI bus. If you verified the ADC value with multimeter then I must have an issue in the code.

I'll keep working on it.

Post Reply