bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

74HC4051 Examples

Thu Feb 09, 2017 8:57 pm

I'm a total noob to multiplexing on the Pi. Does anyone have any example python code for using the 74HC4051.

Backstory, a friend gave me a "Octo (8) MAX31855 thermocouple breakout board for 3.3V systems (type K, K-type)" he had purchased for his Arduino. I have no idea how to make it work on the Pi as all the supporting documentation is written for the Arduino. I figured if I figured out how to change the channels on the Multiplexer I could muddle my way through how to get it to work on the Pi.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 74HC4051 Examples

Thu Feb 09, 2017 9:25 pm

You shouldn't need to use the 74HC4051 to read a MAX31855. The MAX31855 should just talk over the Pi's SPI bus.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 74HC4051 Examples

Thu Feb 09, 2017 9:31 pm

joan wrote:You shouldn't need to use the 74HC4051 to read a MAX31855. The MAX31855 should just talk over the Pi's SPI bus.
Here's a scruffy bit of python code to do exactly that

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855
from time import sleep

def c_to_f(c):
        return c * 9.0 / 5.0 + 32.0
SPI_PORT   = 0
SPI_DEVICE = 0
sensor = MAX31855.MAX31855(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=5000000))

while True:
    temp = sensor.readTempC()
    internal = sensor.readInternalC()
    print('Thermocouple Temperature: {0:0.3F}°C / {1:0.3F}°F'.format(temp, c_to_f(temp)))
    print('    Internal Temperature: {0:0.3F}°C / {1:0.3F}°F'.format(internal, c_to_f(internal)))
    sleep(2)
It uses https://github.com/adafruit/Adafruit-MAX31855-library and https://github.com/adafruit/Adafruit_Python_GPIO
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Thu Feb 09, 2017 11:36 pm

Sorry I didn't give enough detail. I'm reading 8 thermocouples.

Here is a picture of the board.

Image

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Fri Feb 10, 2017 1:27 am

Picture of the back.

Image

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 74HC4051 Examples

Fri Feb 10, 2017 8:20 am

You'll need to drive three extra GPIOs low or high to set the T0, T1 & T2 addressing pins. But other than that the basic Adafruit code will work with the device wired to the normal SPI interface.

http://pinout.xyz/pinout/spi
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Fri Feb 10, 2017 2:23 pm

I attempted a reading not really sure what I am doing wrong. I set T0, T1, and T2 all to out and high. With the thermocouple in spot 7(8) and the following pin out connected. Then ran the Adafruit code below just to see if I could get a reading on one of the probes.

BCM
GND -> GND
T0 -> 22
T1 -> 23
T2 -> 27
V+ -> 3.3V
SO -> 18
CS -> 24
CLK -> 25

Code: Select all

    # software or hardware SPI.
     
    # Raspberry Pi software SPI configuration.
    CLK = 25
    CS  = 24
    DO  = 18
    sensor = MAX31855.MAX31855(CLK, CS, DO)
     
    # Raspberry Pi hardware SPI configuration.
    #SPI_PORT   = 0
    #SPI_DEVICE = 0
    #sensor = MAX31855.MAX31855(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=5000000))

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Fri Feb 10, 2017 2:41 pm

Should I be using the Hardware SPI method vs the Software?

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 74HC4051 Examples

Fri Feb 10, 2017 5:55 pm

bmiller wrote:Should I be using the Hardware SPI method vs the Software?
It doesn't really matter. Hardware SPI is potentially faster and will impose less of a CPU load. Software SPI is more flexible in terms of which GPIO you can use.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 74HC4051 Examples

Sat Feb 11, 2017 12:29 am

My one lone MAX31855 is wired

Vcc --> pin#17
SO --> pin#21
SCK --> pin#23
CS --> pin#24
GND --> pin#20
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: 74HC4051 Examples

Sat Feb 11, 2017 9:06 am

Here is an alternate reader if using hardware SPI. Untested on the device as I don't have one.

Code: Select all

#!/usr/bin/env python

# MAX318555.py
# 2017-02-11
# Public Domain

import time
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

"""
This script reads the temperature of a thermocouple
connected to a MAX31855 SPI chip.
"""

FAULT_D17=16
FAULT_D3=8
FAULT_SCV=4
FAULT_SCG=2
FAULT_OC=1

simDat=[0x64007f00, # 1600.0   127.0
        0x3e806490, # 1000.0   100.5625
        0x064c1900, #  100.75   25.0
        0x01900000, #   25.0     0.0
        0x0000fff0, #    0.0    -0.0625
        0xfffcff00, #   -0.25   -1.0
        0xfff0ec00, #   -1.0   -20.0
        0xf060c900, # -250.0   -55.0
        0xf061c900, # -250.0   -55.0 D17 error
        0xf060c908, # -250.0   -55.0 D3 error
        0xf060c901, # -250.0   -55.0 OC error
       ]

sdp=-1

def simulate():
   global sdp
   sdp += 1
   if sdp >= len(simDat):
      sdp = 0
   return 4, [(simDat[sdp]>>24)&0xff, (simDat[sdp]>>16)&0xff,
      (simDat[sdp]>>8)&0xff, simDat[sdp]&0xff]

pi = pigpio.pi()

if not pi.connected:
   exit(0)

# pi.spi_open(0, 1000000, 0)   # CE0, 1Mbps, main SPI
# pi.spi_open(1, 1000000, 0)   # CE1, 1Mbps, main SPI
# pi.spi_open(0, 1000000, 256) # CE0, 1Mbps, auxiliary SPI
# pi.spi_open(1, 1000000, 256) # CE1, 1Mbps, auxiliary SPI
# pi.spi_open(2, 1000000, 256) # CE2, 1Mbps, auxiliary SPI

sensor = pi.spi_open(0, 1000000, 0) # CE0, 1Mbps, main SPI

stop = time.time() + 60

while time.time() < stop:

   c, d = pi.spi_read(sensor, 4)
   # c, d = simulate()

   if c == 4:

      # Thermocouple temperature
      t = ((d[0]<<8)|d[1])>>2
      if d[0] & 0x80:
         t -= 16384
      t /= 4.0

      # Internal temperature
      int_t = ((d[2]<<8)|d[3])>>4
      if d[2] & 0x80:
         int_t -= 4096
      int_t /= 16.0

      # Fault
      fault = d[3] & 0x0f
      if d[1] & 1: # D17 error
         fault |= FAULT_D17

      if fault == 0: # No fault
         print("t={:.2f} int_t={:.4f}".format(t, int_t))
      else:
         print("fault={}".format(fault))

   time.sleep(0.25) # read 4 times a second.

pi.spi_close(sensor)

pi.stop()

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Sat Feb 11, 2017 2:17 pm

Thanks Ill try it out tonight and see if it works.

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Tue Feb 14, 2017 8:39 pm

I got it working but here is what I had to do and how I connected it to the pi. I attempted the hardware spi method and just couldnt get it to work so I connected via software spi and it works great now.

BCM
GND -- Ground
T0 -- 17
T1 -- 27
T2 -- 22
V+ -- 3.3V
SO -- 18
CS -- 24
SCK -- 25

I set 17, 27, 18 to out, and plugged the thermocouple into position 7 on the board. Then ran the code attached. I was able to read the thermocouple so I tested from 0 to 7 and set 17, 28, 18 to high or low based on the chart on the back of the board and was able to read each thermocouple successfully. Now I need to create a python script that can change the output of the pins and read them automatically with a delay to allow the multiplexer to change to a different position.

Code: Select all


import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855

# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
        return c * 9.0 / 5.0 + 32.0

# Raspberry Pi software SPI configuration.
CLK = 25
CS  = 24
DO  = 18
sensor = MAX31855.MAX31855(CLK, CS, DO)

# Loop printing measurements every second.
print('Press Ctrl-C to quit.')
while True:
    temp = sensor.readTempC()
    internal = sensor.readInternalC()
    print('Thermocouple Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp)))
    print('    Internal Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(internal, c_to_f(internal)))
time.sleep(1.0)

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 74HC4051 Examples

Tue Feb 14, 2017 9:16 pm

This is untested because I don't have the hardware

Code: Select all

import time

import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855
import RPi.GPIO as GPIO

# Define a function to convert celsius to fahrenheit.
def c_to_f(c):
        return c * 9.0 / 5.0 + 32.0

# Raspberry Pi software SPI configuration.
CLK = 25
CS  = 24
DO  = 18
sensor = MAX31855.MAX31855(CLK, CS, DO)
T0 = 17
T1 = 27
T2 = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(T0, GPIO.OUT)
GPIO.setup(T1, GPIO.OUT)
GPIO.setup(T2, GPIO.OUT)
tc = 0
# Loop printing measurements every second.
print('Press Ctrl-C to quit.')
while True:
  if tc == 8:
        tc=0
  GPIO.output(T0, tc & 1<<0)
  GPIO.output(T1, tc & 1<<1)
  GPIO.output(T2, tc & 1<<2)
  temp = sensor.readTempC()
  internal = sensor.readInternalC()
  print('Sensor', tc)
  print('Thermocouple Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp)))
  print('    Internal Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(internal, c_to_f(internal)))
  time.sleep(1.0)
  tc = tc+1
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bmiller
Posts: 11
Joined: Thu Jan 26, 2017 5:34 pm

Re: 74HC4051 Examples

Thu Feb 16, 2017 1:36 pm

Worked great thank you. I did have to move the sleep timer and I also have the outputs on my Pi going to a mysql database.

Code: Select all

    import time

    import Adafruit_GPIO.SPI as SPI
    import Adafruit_MAX31855.MAX31855 as MAX31855
    import RPi.GPIO as GPIO

    # Define a function to convert celsius to fahrenheit.
    def c_to_f(c):
            return c * 9.0 / 5.0 + 32.0

    # Raspberry Pi software SPI configuration.
    CLK = 25
    CS  = 24
    DO  = 18
    sensor = MAX31855.MAX31855(CLK, CS, DO)
    T0 = 17
    T1 = 27
    T2 = 22
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(T0, GPIO.OUT)
    GPIO.setup(T1, GPIO.OUT)
    GPIO.setup(T2, GPIO.OUT)
    tc = 0
    # Loop printing measurements every second.
    print('Press Ctrl-C to quit.')
    while True:
      if tc == 8:
            tc=0
      GPIO.output(T0, tc & 1<<0)
      GPIO.output(T1, tc & 1<<1)
      GPIO.output(T2, tc & 1<<2)
      time.sleep(0.125)
      temp = sensor.readTempC()
      internal = sensor.readInternalC()
      print('Sensor', tc)
      print('Thermocouple Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp)))
      print('    Internal Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(internal, c_to_f(internal)))
      
      tc = tc+1

Return to “General discussion”