Hello, this is is my first time posting on here and I'm new to microelectronics, so excuse my naive questioning.
I want to use an Arduino Uno WiFi Rev2 to steer 20 relays. Specifically, I have 20 locations with temperature sensors and heating elements, and I want to switch on the individual heating elements via the relays when the temperature is not in a given range. I built this: https://arduinogetstarted.com/tutorials/arduino-heating-system#content_about_heating_element_and_ds18b20_temperature_sensor for one such relay and it worked fine, but I struggle with the extension to 20 relays.
First option is to buy a multi-relay-board like this: 8-relay module 5V with optocoupler low-level trigger compatible with Arduino and Raspberry Pi but the Arduino Uno doesn't have 20 pins to connect enough of them.
Second option: My team had PWM Servo Drivers lying around from a previous project, in which we built a similar system but with Peltier heating elements steered via relays connected to these PWM Servo Drivers (Adafruit PCA9685). So, I connected the Servo Driver to the Arduino, and I plugged two individual relays to two channels on the Servo Driver. Otherwise, the wiring is exactly the same as in the tutorial above. Here is the code I used:
//------------------------------------------------------------------------------------------------------------------------------------------------
// Heating system with Arduino Uno WiFi Rev2 & PWM Servo Driver that controls relays for two tubes with resistance heaters and temperature sensors
//------------------------------
// BASICS
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
//-------------------------------
// SETTINGS
// Define number of patches with temperature sensors
const int numberOfDevices = 2;
// Define target temperatures for each device
float targetTemperatures[numberOfDevices] = {30,40};
// Define temperature tolerances
float temperatureTolerance = 0.5;
// Define temperature sensor addresses
uint8_t temperatureSensorAddresses[numberOfDevices][8] = {
{0x28, 0x31, 0xAA, 0x07, 0xD6, 0x01, 0x3C, 0xDD}, // 1 (large metal ring)
{0x28, 0x44, 0x50, 0x07, 0xD6, 0x01, 0x3C, 0xB5} // 2 (small metal ring)
};
// Define the pins on the PWM driver
int devicePins[numberOfDevices] = {0,15};
// Define that data wire is connected to digital pin no. 4
#define ONE_WIRE_BUS 2
// Set up oneWire instance
OneWire oneWire(ONE_WIRE_BUS);
// pass oneWire reference to Dallas Temp sensor
DallasTemperature sensors(&oneWire);
// add the PWM servo driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x60);
//------------------------------
// SETUP
void setup(){
// Start serial
Serial.begin(9600);
// Start the sensors
sensors.begin();
// Start the PWM driver
pwm.begin();
}
//------------------------------
// MAIN
void loop(){
// Get temperatures
sensors.requestTemperatures();
// Loop through all devices
for(int i = 0; i < numberOfDevices; i++){
// get the address of the current sensor
uint8_t currentAddress[8];
for(int j = 0; j < 8; j++){
currentAddress[j] = temperatureSensorAddresses[i][j];
}
// Get temperature of specific sensor
float currentTemperature = sensors.getTempC(currentAddress);
// Report
Serial.print("Temperature of device ");
Serial.print(i);
Serial.print(" : ");
Serial.print(currentTemperature);
Serial.print(" C. ");
Serial.print("Target temperature : ");
Serial.print(targetTemperatures[i]);
Serial.print(" C. ");
// Make decision about heating
if(currentTemperature < (targetTemperatures[i] - temperatureTolerance)){
// If temperature too low, switch heating on and output message
pwm.setPWM(devicePins[i],4096,0);
Serial.println("Switched heating element on.");
} else {
if(currentTemperature > (targetTemperatures[i] + temperatureTolerance)){
// If temperature too high, switch heating off and output message
pwm.setPWM(devicePins[i],0,4096);
Serial.println("Switched heating element off.");
} else {
// Temperature is in the correct interval
Serial.println("Temperature is in range.");
}
}
}
// Wait briefly
delay(500);
}
The readings from the temperature sensor are correct and I get the output message that the heating was switched on, but nothing happens on my relays nor on my heating elements. I've checked the I2C address of the Driver, I've replaced the Driver, and I've checked the wires.
Is it just that PWM Drivers and relays are incompatible? Or am I making an obvious mistake in the code? What would be your solution to this problem?
Thanks in advance!