Page 2 of 2
Re: Analog Pins
Posted: Wed Oct 18, 2017 5:13 pm
by liudr
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 mcp.read(0) 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.
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)
spi.init
cs = Pin(15, Pin.OUT)
cs.value(0)
cs.value(1)
cs.value(0)
# 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")
#print(bin(buffer[2]))
read = bytearray(2)
spi.write_readinto(buffer,read)
cs.value(1)
spi.deinit()
# 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")
Re: Analog Pins
Posted: Fri Sep 27, 2019 12:21 am
by sspaman
Hi,
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.
Regards,
Chris
Re: Analog Pins
Posted: Fri Sep 27, 2019 6:52 pm
by peaktech99
Hi Chris,
I really had to "comb" several books, mainly by the help of
https://books.google.com, to find an explanation that suited my level of knowledge for those so-called "bitwise operations". One that has been helpful was the following:
https://books.google.com/books?id=6OaGAwAAQBAJ&pg=PT638 Also, Paulv's Blog entry has been of great help for me! Please have a look at
http://www.paulvdiyblogs.net/2016/06/si ... 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.
Re: Analog Pins
Posted: Sat Sep 28, 2019 9:51 am
by ghayne
That is a lovely looking piece of kit!
Re: Analog Pins
Posted: Sat Sep 28, 2019 3:07 pm
by sspaman
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.