Terminal output

WiringPi for Raspberry Pi and MAX6675 thermal-couple sensor

by Lewis Loflin

March 10, 2015

WiringPi Blink an LED Demo

YouTube video MAX6675 Raspberry Pi Demo.

WiringPi allows one use Arduino type programming with the Raspberry Pi GPIO. Here I'll explore how this is used and how it differs from Arduino.

I'll connect Raspberry Pi to directly read a MAX6675 thermal-couple sensor, create an executable under C, then call the executable from Python.

This come native to the newest version of Raspbian. One must be root to use.

This is written in C and has to be compiled. I suggest using Geany under Linux.

Get it "sudo apt-get install geany-plugins".

WiringPi was developed by Gordon Henderson.

Also see Simple 3-Wire MAX6675 Thermocouple ADC Arduino Interface

For an introduction example to WiringPi and Geany see WiringPi Blink an LED Demo

WiringPi homepage http://wiringpi.com/

// MAX6675.c read Thermal Couple Sensor

#define CLK 5
#define DBIT 6 // so
#define CS 7

#include <wiringPi.h>
#include <stdlib.h>
#include <stdio.h>

// declare function
int Thermal_Couple_Read(void);

int SENSOR_VALUE = 0;
float Ctemp, Ftemp;

int main()   {
  if (wiringPiSetup () == -1)
    exit (1) ;

  pinMode(CLK, OUTPUT);
  pinMode(DBIT, INPUT);
  pinMode(CS, OUTPUT);

  digitalWrite(CS, HIGH);
  digitalWrite(CLK, LOW);

  SENSOR_VALUE = Thermal_Couple_Read();
  if (SENSOR_VALUE == -1)   {
    printf ("No sensor connected. \n");
  }
  else   {
    printf ("S %d ", SENSOR_VALUE);
    Ctemp = SENSOR_VALUE * 0.25;
    printf ("C = %4.2f ", Ctemp);
    Ftemp = (Ctemp * 9 / 5) + 32;
    printf ("F = %4.2f", Ftemp);
  }

  return 0;
}

int Thermal_Couple_Read()   {

  int value = 0;
  // init sensor
  digitalWrite(CS, LOW);
  delay(2);
  digitalWrite(CS, HIGH);
  delay(200);

  /* Read the chip and return the raw temperature value
  Bring CS pin low to allow us to read the data from
  the conversion process */

  digitalWrite(CS, LOW);
  /* Cycle the clock for dummy bit 15 */
  digitalWrite(CLK, HIGH);
  // delay(1);
  digitalWrite(CLK, LOW);

  /*
  Read bits 14-3 from MAX6675 for the Temp.
  Loop for each bit reading
  the value and storing the final value in 'temp' */

  int i;
  for (i = 14; i >= 0; i--) {
    digitalWrite(CLK, HIGH);
    // delay(1);
    value += digitalRead(DBIT) << i;
    digitalWrite(CLK, LOW);
  }

  // check bit D2 if HIGH no sensor
  if ((value & 0x04) == 0x04) return -1;
  // shift right three places
  return value >> 3;

}

Here is a partial code for the Python part. The full code is at max6675Tkinter.txt. Rename this from a txt file to a py file.


def readMAX6675(self):
	# get test string from MAX6675 executable
	f = os.popen("sudo /root/work/MAX6675")
	s1 = f.read()

	# will return a string such as
	# S 97 C = 24.25 F = 75.65
	print s1

	pos1 = 0
	pos1 = s1.find('C = ')
	print "Char C is at position ", pos1 
	
	pos2 = 0
	pos2 = s1.find('F = ')
	print "Char F is at position ", pos2

	Sensor = int(s1[2:pos1 - 1])
	print "Sensor value = ",  Sensor

	degC = float(s1[pos1+3 : pos2-1]) 
	print "Degrees C = ", degC

	degF = float(s1[pos2+3 : len(s1)])
	print "Degrees F = ", degF
	self.result_var.set(degF)

The above is seen on the terminal when executed from Geany.




 


donate