Compare commits

...

2 Commits

@ -0,0 +1,131 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "42e3a72f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T1 = 27.73 °C\n"
]
}
],
"source": [
"import serial\n",
"import time\n",
"\n",
"ser = serial.Serial('/dev/cu.usbmodem101', 115200, timeout=1) # Adjust port\n",
"time.sleep(2) # Wait for Arduino reset\n",
"\n",
"# Example: read temperatures\n",
"ser.write(b'T1\\n')\n",
"t1 = ser.readline().decode().strip()\n",
"time.sleep(1) # Wait for Arduino reset\n",
"print('T1 =', t1, '°C')\n",
"ser.close()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4fe435c4",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"Temps = np.ones(10)*40\n",
"Reads = np.zeros(10)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "4be8126a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"T1 = 26.86 °C\n",
"T1 = 26.82 °C\n",
"T1 = 26.89 °C\n",
"T1 = 26.79 °C\n",
"T1 = 26.82 °C\n",
"T1 = 26.79 °C\n",
"T1 = 26.89 °C\n",
"T1 = 26.79 °C\n",
"T1 = 26.89 °C\n",
"T1 = 26.82 °C\n"
]
}
],
"source": [
"import serial\n",
"import time\n",
"\n",
"ser = serial.Serial('/dev/cu.usbmodem101', 115200, timeout=1) # Adjust port\n",
"time.sleep(2) # Wait for Arduino reset\n",
"\n",
"for i in range(len(Temps)):\n",
" value = Temps[i]\n",
" command = f\"Q1 {value}\\n\"\n",
" ser.write(command.encode('ascii'))\n",
" ser.write(b'T1\\n')\n",
" t1 = ser.readline().decode().strip()\n",
" Reads[i] = t1\n",
" print('T1 =', t1, '°C')\n",
" time.sleep(1) # Wait for Arduino reset\n",
"\n",
"ser.close()\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "db140260",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([26.86, 26.82, 26.89, 26.79, 26.82, 26.79, 26.89, 26.79, 26.89,\n",
" 26.82])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Reads"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv (3.13.5)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

@ -1,46 +0,0 @@
#include<Arduino.h>
// vars and pins at TCLab
const int LED = 9;
const int T1 = A0; // temperature sensor T1 input
const int T2 = A2; // temperature sensor T2 input
const int Q1 = 3; // PWM at Q1 (output)
const int Q2 = 5; // PWM at Q2 (output)
// vars and constants:
int T1Val;
int T2Val;
const int nAvg = 10; // samples per reading
const long BAUD = 115200; // Freq serial comm
const float factor = 3.3/1024*100; // 3.3V/1024
// functions
float readDeg(int channel) {
float acc = 0;
for(int i=0;i<=nAvg;i++) {
acc += analogRead(channel)*factor-50;
}
return acc/nAvg;
}
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
analogReference(EXTERNAL);
Serial.begin(BAUD);
delay(100);
Serial.println("Temps");
}
void loop() {
// put your main code here, to run repeatedly:
float t1Val = readDeg(T1);
float t2Val = readDeg(T2);
Serial.print(t1Val,4); Serial.print(',');
Serial.println(t2Val,4);
delay(250);
}

@ -0,0 +1,77 @@
#include<Arduino.h>
// vars and pins at TCLab
const int LED = 9;
const int T1 = A0; // temperature sensor T1 input
const int T2 = A2; // temperature sensor T2 input
const int Q1 = 3; // PWM at Q1 (output)
const int Q2 = 5; // PWM at Q2 (output)
// vars and constants:
const int nAvg = 10; // samples per reading
const long BAUD = 115200; // Freq serial comm
const float factor = 3.3/1024*100; // 3.3V/1024*100
// functions
float readDeg(int channel) {
float acc = 0;
for(int i=0;i<=nAvg;i++) {
acc += analogRead(channel)*factor-50;
}
return acc/nAvg;
}
void setHeater(int pin, float value) {
// clamp the input value
if(value<0) value = 0;
if(value>100) value = 100;
//convert to pwm 0 - 255
int pwm = (int)(value*255.0/100.0+0.5);
analogWrite(pin, pwm);
analogWrite(LED, pwm);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(BAUD);
pinMode(Q1, OUTPUT);
pinMode(Q2, OUTPUT);
pinMode(LED, OUTPUT);
analogReference(EXTERNAL);
delay(100);
//Serial.println("TCLab ready");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if(cmd == "T1"){
float T1Val = readDeg(T1);
Serial.println(T1Val);
}
else if(cmd == "T2"){
float T2Val = readDeg(T2);
Serial.println(T2Val);
}
else if(cmd.startsWith("Q1 ")){
int val = cmd.substring(3).toInt();
setHeater(Q1, val);
}
else if(cmd.startsWith("Q2 ")){
int val = cmd.substring(3).toInt();
setHeater(Q2, val);
}
else if(cmd == "R1"){
Serial.println("Not yet");
}
else {
Serial.println("Command Error");
}
}
}
Loading…
Cancel
Save