Page 2 of 2

Re: Analog Pins

Posted: Wed Oct 18, 2017 5:13 pm
by liudr
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.

Re: Analog Pins

Posted: Tue Jul 30, 2019 8:20 pm
by peaktech99
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!

Re: Analog Pins

Posted: Thu Aug 01, 2019 7:31 pm
by peaktech99
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")