Jump to content

Need help with simple 'once every hour' program

Recommended Posts

Hey all, I'm having some trouble with a project that checks a pin every hour and should trip a relay if a certain pin is HIGH.


Some notes:

- this is with the MSP430G2553 chip

- it seems to run fine if duration is low (ex: 5000ms)

- it worked at least once (triggered buzzer after an hour) when I replaced the relay circuit with a simple buzzer


I'm using this relay circuit:


And this code using TwoMsTimer library:

#include <TwoMsTimer.h>
unsigned long duration = 3600000;
int relayPin = 14;
int moistPin = 15;
int state = 0;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(moistPin, INPUT);
  digitalWrite(relayPin, LOW); // necessary bc energia sets green led HIGH by default?
  TwoMsTimer::set(duration, checkMoisture);

void loop() {
  if (state == 1) {
     state = 0; // set state back to zero
     digitalWrite(relayPin, HIGH);
     digitalWrite(relayPin, LOW);

void checkMoisture() {
  if(digitalRead(moistPin) == HIGH) {
     // set state to 1 to trip relay next time loop runs
     state = 1;

Any help is much appreciated.




Edited by mattanonymous
Link to post
Share on other sites

Thanks for the tip Rei Vilo. I knew the pullup was necessary for push buttons, but didn't consider using it for a normal digitalRead (makes sense now, though).


@@roadrunner84 I agree, but one thing at a time. I'm trying to get it running reliably before I start with the fancy stuff. I've been bitten by premature optimization too many times in the past  :tongue:


UPDATE: I changed 'moistPin' mode to INPUT_PULLUP and that seemed to fix it since it triggered the relay after an hour. However, the relay was not triggered ever again after that. I hit the reset button this morning and expected it to trigger the relay after an hour, but no luck :/


Any ideas?

Link to post
Share on other sites

Your circuit diagram shows the input to the MCU but not the output circuit. Are you by chance driving the buzzer/relay directly from a GPIO pin? If you are, two things.


1) This would explain the buzzer working once and not the relay. Buzzers need a lot less current and can be driven directly depending on the buzzer. Relays need more than a GPIO pin can provide.

2) Relays are inductive so if you drive them with a GPIO without a protection diode, the voltage kick at turn off will fry your output pin. This might explain why you said it worked once. Is the buzzer no longer working? You might have fried that pin.

Link to post
Share on other sites

The resistor value on the base of that transistor looks a bit high. I don't know off hand the max current from a gpio pin but you probably need to draw about 10 ma or more to saturate the transistor. Would try a 330 ohm resistor. If that doesn't work,find out how much current your relay draws at your working voltage.
Another thing to check is the collector voltage when you have the gpio pin high. If the transistor is saturated, it should be around 0.3V.


EDIT: Another thing is to check each part of your project (software and hardware) separately. Don't try to debug everything at once. Test that your relay circuitry is working using a simple blink program-- instead of an LED, "blink" your relay. Test your input circuit by mirroring the input pin to one of the onboard LEDs (or the relay if you know it is working). Only then debug your program-- timers, low power modes, etc. Otherwise you have no way of narrowing down the problem.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...