Project works on USB but not DC power

Hello,
I'm new to the forum and Arduino in general, I wrote my very first code a few months ago now I'm already on my third Arduino project, it's been a bit addicting.

My current project involves reading a temperature from a Dallas thermometer to regulate a heating element via a relay. I have an LCD that displays the current temperature, and two buttons to allow input of what temperature you want.

I wired everything up and coded it, and it works perfectly, as long as it is powered through the USB port. If I unplug the USB with battery for backup, it still works fine. After resetting it and running on only 9V battery, the buttons don't work properly anymore, they don't register most hits, and there is a second delay between each time it registers. When it works right I can hold the button and it increases the variable quickly. Switching back to USB power it still has the same problem. Only when reuploading the code does it start working properly again.

I couldn't narrow it down between a hardware or program issue so I posted it here. I suspected the battery and tried powering with a 12V computer PSU, same issue. I ran all of the ground connections directly to the DC ground since I plan to run too big of a current for the ground pin, not sure if that could hurt.

Here's the code I'm using.

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

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
float temp = 0;
int tempu = 20;
int up = 6;
int down = 7;
int buttonState = 0;
int buttonState2 = 0;

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

// 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);

DeviceAddress insideThermometer = { 0x28, 0x4F, 0x54, 0xBF, 0x03, 0x00, 0x00, 0x2A };



void setup(void)
  {
    pinMode(led, OUTPUT);
    pinMode(relay, OUTPUT);
    pinMode(up, INPUT);
    
  Serial.begin(9600);
  sensors.begin();
  sensors.setResolution(insideThermometer, 10);
  lcd.begin(16, 2);
}

void printTemperature(DeviceAddress deviceAddress)
 { float tempC = sensors.getTempC(deviceAddress);
 temp = tempC;
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } 
  else {
    Serial.print(temp);
  }

}

void loop(void){
  buttonState = digitalRead(up);
  if (buttonState == HIGH) {    
    tempu ++;  
  }
   if (buttonState2 == HIGH) {    
    tempu --;  
  }
  sensors.requestTemperatures();
  printTemperature(insideThermometer);
  Serial.print("\n\r");
  lcd.setCursor(0, 0);
  lcd.print("Temp ");
  lcd.print((char)223); 
  lcd.print("C:");
  lcd.setCursor(10, 0);
  lcd.print(temp);
  lcd.setCursor(0, 1);
  lcd.print("Target:");
  lcd.setCursor(10, 1);
  lcd.print(tempu);
  }

Are you talking about one of this little 9V batteries? Their current capacity is pathetic, avoid them whenever possible.

Do you have a multimeter? Monitor the Vin and 5V node on the Arduino when using "DC Power." If your circuit is drawing too much current you'll see one or the other drop significantly.

You know what you should do, i'd never use a 9v battery, go to your local electronics store, buy one of them little battery holders, get a 4AA box (4x 1.5v in series = 6volts) connect that to your arduino the thing will run for years on end providing you don't use anything that sucks up the current (eg a power hungry sensor or IC..... wiznet and alcohol sensors spring to mind :open_mouth: )

or get one of those big rechargable NiCd packs that people use for model-race-cars.

Hi,

Though the 9V battery may be playing a part I see there's no debounce on your button code, so the two events may be somewhat coincidental. If you've not come across this before, check this video Tutorial 02 for Arduino: Buttons, PWM, and Functions - YouTube (I've started it at 10:39 where he shows switch bounce, and why it happens, then a simple remedy).

The fact you say it works when you hold the button was what caught my attention.

Cheers !
Geoff

I thought the battery might cause it, that is why I tried it with a 500 watt computer power supply with the same issue, so I know it isn't the battery.

I was thinking about debounce, however my problem is that it doesn't register enough, not that it registers too much. I want to input around 20-50 clicks at a time, so holding to do multiple is actually a positive for me.

ilija:
I thought the battery might cause it, that is why I tried it with a 500 watt computer power supply with the same issue, so I know it isn't the battery.

ATX Supplies generally require a minimum load (around 1 AMP on the 5V and 12V supplies). Did you properly load the supply?

This is why it is important to actually look at what the voltages are with a multimeter.

I've checked the readings with the multimeter. The 5V is a steady 4.98V on both USB and DC power. The Vin is 4.46 on USB and 6.9 on DC power, there is no voltage drop during operation.

I just tried some more things with it, my original question was misleading. When I restart the arduino, it has problems regardless of whether it is using USB or DC power. I was just always restarting with DC and thought that was were the problem was.

Is there any reason the arduino would act differently on a restart compared to freshly uploaded code?

I took out the button part of the code, now I just have it increment the counter once every time the program loops. It increments about 5 times per second when the code is uploaded, after restarting it is slowed down to 1 times a second.

I tried it without the one wire and temperature library and now then it works at proper speed.
I also tried it with the libraries in the code, and physically disconnecting the thermometer, also no problems.

When the thermometer is plugged in, it runs very slow. When pulling up the serial monitor on the computer, it starts working at full speed again.