Temperature Sensors triggering Relay project issues

Hello, I’ve been dabbling with Arduino for a little bit now, this is my most advanced project to date.

Project Goal: 2 DS18b20 sensor’s reading 2 temperatures controlling a dual relay module. This is to control a seedling propagation environment by maintaining temperature via a heat mat controlled by Arduino relay. Also include bluetooth so that I can monitor temperatures remotely.

What works:
I can successfully bluetooth the values of two separate DS18B20’s on the same bus wire to my phone and read them Individually (See screenshot attached)

What Doesn’t work:
The Relay is being really strange. Initially, if I plug the Relay Power and ground into the same Ground Rail as the Bluetooth and Temperature Sensors, then the temp reading goes to 185.00 on both and sticks there until I reflash a sketch. Pulling the power doesn’t release it.

I figure I’m overloading it, so I’ve gotten around that temporarily by using an arduino nano on its own breadboard, and just plugging the Relay into that +/- rail. So now I can get correct Temperature readings out of both, with relay connected.

See code below, this is the best I can get. This somehow turns the relay on for a little bit, less than a minute…and its independent of what the Water Temperature probe’s if statement for > 85.

// Coded by theratdude64 to maintain an ideal seedling/cutting propagation environment

// Hardware setup:
// BT module   Arduino
    // GND ------- GND
    // VCC ------- 5V
    // TX  ------ RX
    // RX ------ TX
// OneWire Temperature sensor pin 2 (a 4.7K resistor is necessary)

#include <OneWire.h>
#include <DallasTemperature.h>

const int levelSensor = 2; // Water Level Float Sensor
const int lowLED = 4; // Low Water Blue LED - 
const int led1Pin = 5; //Too Cold Red LED
const int led2Pin = 6; //Too Hot Green LED
const int relay1Pin = 7; //Top Outlet
const int relay2Pin = 8; // Bottom Outlet

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup Bluetooth
#define bluetooth Serial

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Assign the unique addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress waterTemp = { 0x28, 0xFF, 0xC8, 0x36, 0xC4, 0x17, 0x04, 0x3B };
DeviceAddress domeTemp = { 0x28, 0xFF, 0x05, 0x3C, 0xC4, 0x17, 0x04, 0xEE };

void setup(void)
  // start serial port
  pinMode(relay1Pin, OUTPUT);
  pinMode(relay2Pin, OUTPUT);
  // Start up the library
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(waterTemp, 10);
  sensors.setResolution(domeTemp, 10);

void printWater()
  float tempC = sensors.getTempC(waterTemp);
  if (tempC == -127.00) {
    Serial.print("Error getting Water temperature");
  } else {
   // Serial.print("C: ");
   // Serial.print(tempC);
   // Serial.print("Water Temp: ");
   // Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.println( (String)"Water Temp: " + DallasTemperature::toFahrenheit(tempC) + "\n" );
  bluetooth.print( (String)"Water Temp: " + DallasTemperature::toFahrenheit(tempC) + "\n" );
 // if (DallasTemperature::toFahrenheit(tempC) > 85){ digitalWrite(relay2Pin, HIGH); }

void printDome()
  float tempC = sensors.getTempC(domeTemp);
  if (tempC == -127.00) {
    Serial.print("Error getting Dome temperature");
  } else {
    //Serial.print("Dome Temp: ");
   // Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.println( (String)"Dome Temp: " + DallasTemperature::toFahrenheit(tempC) + "\n" );
    bluetooth.print( (String)"Dome Temp: " + DallasTemperature::toFahrenheit(tempC) + "\n" );

int waterValue(DeviceAddress)
float tempC = sensors.getTempC(waterTemp);
float waterF = DallasTemperature::toFahrenheit(tempC);
return waterF;

void loop(void)

int waterMat = waterValue(waterTemp);

  //Serial.print("Getting temperatures...\n\r"); 
//  Serial.print("Water temperature is: ");
 // Serial.print("\n\r");
 // Serial.print("Dome temperature is: ");
 // Serial.print("\n\r\n\r");

if (waterValue(waterTemp) > 85){
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, HIGH);

If you are using the 27115 relay, which is the inexpensive blue electro-mechanical relay, it typically requires 85mA to operate. While the Arduino may trigger it, there is simply not enough current to operate. Either insert a transistor and provide it it's own power or look into a solid state relay such as the LCA715.

Yes this is a double little blue cheapie relay. I've also got an Elegoo Power MB V2 that I can use to power things too

You can't do it with the nano. If you have a UNO powered by a wall wort with the 2.5mm jack you could take power off Vin. The Elegoo Power MB V2 is good for about 700mA but you must power it separately (not through the nano). In either case, you need a transistor (2N2222 enough) to switch the power to the relay control, use the MCU pin attached through about a 10K resistor to the base of the transistor. Use about a 120ohm limiting resistor for the load.

I’ve got this Elegoo Power MB v2 that I can use that should be more powerful, see the attached data sheets. I also want to run an RTC module. Will this power this project well?

I just can’t find a power cord for that :-X And the specs do not show the barrel jack’s polarity whether the pin is +/- so I don’t even know what to get. Does this data sheet show that to someone more adept at reading them than I?

Does my code look ok?

Addicore YwRobot Breadboard Power Supply.pdf (152 KB)

mb_102.pdf (1.02 MB)

That power module is not a problem, I have several and you can drive it with a USB connector, limited to 500mA as is the USB spec. The issue is getting 85 or so milliamps to the relay which you can't supply with any MCU that I'm aware of. Get the transistor.

The RTC will not be a problem, most will take 5V (check first usually they have a 3.3V regulator on board).

Have a look at the wiring diagram on this site

You see how the power for the relay is taken off the Arduino's power pin, not controlled directly by the logic pin. The diode is useful/necessary to avoid any back EMF from the solenoid coil. With a relay, not such an issue, if a motor, a must.

A couple other useful articles.

You know, you can get the same information by Googling 'controlling a relay with arduino' and save me all this typing.

The nano I'm using was part of a 3 pack, and one of the others has been powering a relay and a single DS18b20, its been doing fine I am just upgrading the project. Running into power problems I haven't experienced before.

Thanks for the links, I'll check em out :slight_smile:

As you've got them, do you know the polarity of the barrel jack? The USB Port on it says in bold red header "DO NOT ATTEMPT TO POWER USING USB" as its for powering external devices. Darn, there went that idea.

On the male part (the bit that sticks into the female part, on the board) the outside bit (that you can easily grab) is negative.

All power calculations are edge cases. Each pin is supposed to be able to source/sink 40mA so long as the total doesn't exceed 200. The relays, according to datasheet, are typically 85mA. Having said that, I've run a bank of 8 directly off the pins, but the output is not reliable.

Ah that clears things up, thankya much :slight_smile: +1 Karma

Does my code look like it'll run it properly once power woe's sorted out?

I found a Barrel connector that fits perfectly that is chopped off, and a chopped USB cable. I can splice them together to make a custom USB DC Barrel cable today lol

If your going to splice cables, triple check polarity.

Oh for sure, I've got a Fluke 87v I'll use.

To clarify, My Relay is a module, not just a direct relay:

It has been working on a Nano installed on a breadboard fine. Just not liking multiple devices.

Those relay modules usually energize the relay when the "In" pin is pulled LOW, I don't see anywhere in your code where you digitalWrite() either relay pin LOW. Post a wiring diagram.

Hmm I thought it was energizing the pin to High?

if (waterValue(waterTemp) > 85){
  digitalWrite(relay1Pin, HIGH);
  digitalWrite(relay2Pin, HIGH);

^ There is where I'm hoping to trigger them, but it's at High.

I am making a Fritzing Diagram of the setup but I'm at work currently and don't have that arduino with me today. I can later though

OK, but "Fritzing" is kind of a bad word on the forum because it don't show pin / terminal numbers and criss-crossing wires can be confusing, if you can, just make a hand drawn diagram with as much detail as possible, take a picture or scan of it and post that, or Fritz as a last resort. :slight_smile:
Also both relays together will draw about 150 mA, maybe enough to pull the voltage on the 5V pin down enough to cause a reset, a separate 5V supply for the relay module would be better.

A quick little circuit for using a 2N2222