Hello my dears,
I am building a "simple" circuit based on the Arduino Micro to control the temperature in a box. The principle is simple:
- SHT85 sensor to read the temperature
- 4 power resistors to generate heat (25W, 1.5 Ohm) on a 24V power supply
- PID controller with 2 transistors (BC337 and IRFZ44NPBF) to PWM control the 24V
The circuit without connected devices looks as follows:
...and with connected devices:
The power supply is connected to a regular outlet with no ground wire (German outlet). The 24V go into U5 in the diagram. U4 takes the power resistors. The SHT85 gets the power and ground from U3 and the SCL and SDA lines are connected to U2. The Arduino gets its power from my laptop, which is connected to its power supply.
The BC337 is used to fire the IRFZ44NPBF which inverts the logic: when BC337 is on, the IRFZ44NPBF is off and the other way around.
My problem is that when I do not connect the 24V power supply, the sensor readings work well. As soon as I connect the power supply, some readings fail and the Arduino even freezes from time to time. I suspect it may be an issue with ground between the Arduino and the power supply.
Here is the code:
#include <Wire.h>
#include "SHTSensor.h"
#include <PID_v1.h>
#define PIN_OUTPUT 6
SHTSensor sht;
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
double Kp = 5, Ki = 0, Kd = 0; //Kp=50, Ki=1, Kd=5;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
void setup() {
pinMode(PIN_OUTPUT, OUTPUT);
Wire.begin();
Serial.begin(9600);
delay(1000);
if (sht.init()) {
Serial.print("init(): success\n");
} else {
Serial.print("init(): failed\n");
}
sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM); // only supported by SHT3x
if (sht.readSample()) {
Input = sht.getTemperature();
Setpoint = 30; // desired temperature
} else{
Serial.println("Failed to init PID");
}
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop() {
if (sht.readSample()) {
String temp = String(sht.getTemperature(), 2);
String hum = String(sht.getHumidity(), 2);
String per = String(Output, 2); // percentage of max. voltage
Serial.println(temp + " " + hum + " " + per);
Input = sht.getTemperature();
myPID.Compute();
analogWrite(PIN_OUTPUT, 255-Output);
} else {
Serial.print("Error in readSample()\n");
}
delay(1000);
}
Edit:
I designed a PCB (which is actually my first PCB). Don't get confused by the lower right part of it. I intended to control two lines of resistors. I removed these from the schematic to reduce confusion. The ground of the PCB is on the lower side, which is connected to the Arduino ground.