Skip to content

Instantly share code, notes, and snippets.

@NT7S
Created May 18, 2014 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NT7S/70961fd72c516ad38e35 to your computer and use it in GitHub Desktop.
Save NT7S/70961fd72c516ad38e35 to your computer and use it in GitHub Desktop.
Thermal Chamber
import usbtmc
import serial
import time
import sys
import math
temperature = 0.0
def printparams():
# Get the elapsed time
elapsed = time.time() - clock_start
elapsed = math.floor(elapsed)
# Get the frequency
sa.write(":CALCulate:MARKer1:PEAK:SET:CF")
time.sleep(2)
freq = sa.ask(":CALCulate:MARKer:FCOunt:X?")
# Get the temperature
ser.write('t')
t = float(ser.readline().strip())
# Print it out in comma delimeted format
print '{},{},{}'.format(elapsed, t, freq)
return t
# Arduino serial dev paramaters
DEVICE = '/dev/ttyUSB0'
BAUD = 9600
# Open the DG1022 arb and config for reading frequency
sa = usbtmc.Instrument(0x1ab1, 0x0960)
# Preset
sa.write(":SYST:PRES")
time.sleep(1)
# Set the ref to +20 dBm
sa.write(":DISP:WIN:TRAC:Y:SCAL:RLEV 20")
time.sleep(1)
# Peak Search
#sa.write(":CALCulate:MARKer1:STATe ON")
sa.write(":CALCulate:MARKer1:PEAK:SET:CF")
time.sleep(1)
sa.write(":FREQuency:SPAN 1000000")
time.sleep(1)
sa.write(":CALCulate:MARKer1:PEAK:SET:CF")
time.sleep(1)
sa.write(":FREQuency:SPAN 10000")
time.sleep(1)
sa.write(":CALCulate:MARKer1:PEAK:SET:CF")
time.sleep(1)
sa.write(":FREQuency:SPAN 1000")
time.sleep(1)
sa.write(":CALCulate:MARKer1:PEAK:SET:CF")
# Set the marker for frequency counting
sa.write(":CALCulate:MARKer:FCOunt:STATe ON")
sa.write(":CALCulate:MARKer:FCOunt:RESolution 1")
time.sleep(3)
# Open serial port
try:
ser = serial.Serial(port=DEVICE, baudrate=BAUD, timeout=2)
except:
print 'Cannot open serial port'
sys.exit(0)
ser.write('t')
ser.readline()
# Start the clock
clock_start = time.time()
# Take the first reading
temperature = printparams()
# Turn on the light and fan
ser.write('L')
ser.write('F')
# Main processing loop
while temperature < 60.0:
time.sleep(30)
temperature = printparams()
# Turn off light
ser.write('l')
# Main processing loop
while temperature > 35:
time.sleep(30)
temperature = printparams()
# Turn off fan
ser.write('f')
#include <Wire.h>
#include <OneWire.h>
const int fan_relay = 7;
const int light_relay = 6;
OneWire ds(10); // on pin 10
void setup(void) {
Serial.begin(9600);
ds.reset();
pinMode(fan_relay, OUTPUT);
pinMode(light_relay, OUTPUT);
}
unsigned int readBytes(int count)
{
unsigned int val = 0;
for (int i = 0; i < count; i++)
{
val |= (unsigned int)(ds.read() << i * 8);
}
return val;
}
float readTemp(void)
{
byte temp_read = 0;
unsigned int count_remain = 0;
unsigned int count_per_c = 0;
byte configuration_register = 0;
ds.reset();
ds.write(0xEE); //Start Converting the temperatures
do {
delay(1);
configuration_register = 0;
ds.reset();
ds.write(0xAC);
// Read the configuration Register from the DS1821
configuration_register = readBytes(1);
} while ((configuration_register & (1 << 7)) == 0); // If Bit #8 is 1 then we are finished converting the temp
// Get Temp
ds.reset();
ds.write(0xAA);
temp_read = readBytes(1); ;
// Get Count Remaining
ds.reset();
ds.write(0xA0);
count_remain = readBytes(2);
// Load The Counter to populate the slope accumulator
ds.reset();
ds.write(0x41);
// Read Count Per Deg
ds.reset();
ds.write(0xA0);
count_per_c = readBytes(2);
// If we are reading above the 200 mark then we are below 0 and need to compensate the calculation
if (temp_read >= 200) temp_read -= 256;
float highResTemp = (float)temp_read - .5 + (((float)count_per_c - (float)count_remain) / (float)count_per_c);
// highResTemp = (float)((1.80 * highResTemp) + 32.00);
Serial.println(highResTemp);
}
void loop(void)
{
unsigned int rx_byte;
if (Serial.available() > 0)
{
rx_byte = Serial.read();
switch(rx_byte)
{
case 'L':
digitalWrite(light_relay, HIGH);
break;
case 'l':
digitalWrite(light_relay, LOW);
break;
case 'F':
digitalWrite(fan_relay, HIGH);
break;
case 'f':
digitalWrite(fan_relay, LOW);
break;
case 'T':
case 't':
readTemp();
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment