Digital ouput pin switches uncontrollably

Hi all!

I have hooked up a 4-relay board to my Arduino Uno. I use pin 1 to control a LED strip, which flashes randomly (time ON anywhere between 0.1-0.8 seconds, time OFF anywhere between 0.05 and 0.5 seconds). During the first moments it works well, but after a while (typically after 10-50 seconds) the output pin seems to shoot to some faulty mode whereby the pin switches quite fast. The multimeter says about 4.7kHz, resulting in 2.5V so apparently 50/50 duty cycle of HIGH/LOW.

Here is a video of how it should work:

And a video of the faulty mode:

Note: I'm not using the UART and I tried other output pins.

The normal switching behaviour is controlled by a keypad and I can shut off the switching behaviour. But as the faulty mode has established, it will turn back to that faulty mode soon as the switching behaviour is turned on again.

Does anyone has an idea what's happening here? And how to solve it?

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] =
{
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};
byte rowPins[ROWS] = {3, 8, 7, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 2, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

////LOW on pin turns on LEDs
const int led_red = 12;
const int led_green = 13;
const int t_int = 100;

const byte relay_tl_light = 1; // +5V-----LED anode/cathode-----220R-----pin 9
const byte relay_smoke = 0; // +5V-----LED anode/cathode-----220R-----pin 8
const byte relay_cable_out = 9;
const byte relay_cable_in = 10;

bool bool_relay_tl_light = false;

unsigned int ts_base = millis();
unsigned int interval_duration = 0;

int state_led = 0;

//Target password
unsigned int PassWord = 611;

//Password number entered
unsigned int PW;

bool PWokay = false;

//******************************************************
void setup()
{
  pinMode(led_red, OUTPUT);
  pinMode(led_green,  OUTPUT);
  pinMode(relay_tl_light, OUTPUT);
  pinMode(relay_smoke, OUTPUT);
  pinMode(relay_cable_out, OUTPUT);
  pinMode(relay_cable_in, OUTPUT);

  digitalWrite(led_red, LOW);
  digitalWrite(led_green,  LOW);
  digitalWrite(relay_tl_light, LOW);
  digitalWrite(relay_smoke, LOW);
  digitalWrite(relay_cable_out, LOW);
  digitalWrite(relay_cable_in, LOW);

  keypad.addEventListener(keypadEvent); //Add an event listener for this keypad
} //END of            s e t u p ( )

//******************************************************
void loop()
{
  char key = keypad.getKey();

  if (key)
  {

    //check for a valid password
    if ((key == '*' || key == '#') && PWokay == false)
    {
      if (PW == PassWord)
      {
        PWokay = true;

        digitalWrite(led_green, HIGH);
        digitalWrite(led_red, LOW);
        digitalWrite(relay_smoke, HIGH);
      }

      else
      {
        PWokay = false;
        PW = 0;

        digitalWrite(led_green, LOW);
        for (int i = 0; i < 3; i++) {
          digitalWrite(led_red,  LOW);
          digitalWrite(led_green, LOW);
          delay(t_int);

          digitalWrite(led_red,  HIGH);
          digitalWrite(led_green, LOW);
          delay(t_int);
        }
        digitalWrite(led_red,  LOW);
      }
    }

    //assemble the password number
    if (PWokay == false && key >= '0' && key <= '9')
    {
      digitalWrite(led_green, HIGH);
      delay(50);
      digitalWrite(led_green, LOW);

      PW = PW * 10 + (key - '0');
    }
  }

  if (millis() - ts_base >= interval_duration && PWokay == true) {

    if (1) {
      if (bool_relay_tl_light) {
        digitalWrite(relay_tl_light, HIGH);

        // Light turned on
        interval_duration = random(200, 1000);
      }
      else {
        digitalWrite(relay_tl_light, LOW);

        // Light turned off
        interval_duration = random(100, 500);
      }
    }


    ts_base = millis();
    bool_relay_tl_light = !bool_relay_tl_light;
  }

} //END of            l o o p ( )


//******************************************************
void keypadEvent(KeypadEvent key)
{
  switch (keypad.getState())
  {
    //*********************
    //case PRESSED:
    //  {
    //  }
    //  break; //END of PRESSED

    //*********************
    case RELEASED:
      {
        switch (key)
        {
          //***************
          //only if there is a valid password
          case '*':
            if (PWokay == true)
            {
              {
                //LED OFF
                digitalWrite(relay_cable_in, LOW);
              }
            }
            break;

          //***************
          //only if there is a valid password
          case '#':
            if (PWokay == true)
            {
              {
                //LED OFF
                digitalWrite(relay_cable_out, LOW);
              }
            }
            break;

        } //END of switch (key)
      }
      break; //END of RELEASED

    //*********************
    case HOLD:
      {
        switch (key)
        {
          //***************
          //only if there is a valid password
          case '*':
            if (PWokay == true)
            {
              {
                //LED ON
                digitalWrite(relay_cable_in, HIGH);
              }
            }
            break;

          //***************
          //only if there is a valid password
          case '#':
            if (PWokay == true)
            {
              {
                //LED ON
                digitalWrite(relay_cable_out, HIGH);
              }
            }
            break;

          //***************
          //reset the lock
          case '0':
            {
              PWokay = false;
              PW = 0;

              digitalWrite(led_green, LOW);
              digitalWrite(led_red, LOW);
              digitalWrite(relay_tl_light, LOW);
              digitalWrite(relay_smoke, LOW);
              digitalWrite(relay_cable_out, LOW);
              digitalWrite(relay_cable_in, LOW);
            }
            break;

        } //END of switch (key)
      }
      break; //END of HOLD

  } //END of switch (keypad.getState())

} //END of    k e y p a d E v e n  t ( )

//******************************************************

Welcome to the forum

It would help a lot if you posted your code, using code tags when you do, and a schematic of your project so that it is possible to see how things are actually connected

Which mode is causing the problem ?

You should not use pins 0 and 1, because these pins are used for the serial connection to the PC.

1 Like

I'm not using the serial connection in any way in which case pins 0 and 1 can be used as digital output pins normally. Also I have tried pin 11 which didn't solve the problem.

Thanks for your reply. I have added a schematic and the code to the first post.

You must be aware, that you cannot do nearly any debugging if you use pin 0 and 1 for other purposes. Serial.print() is the main tool for debugging your code.

the variable you are using for timing is defined as an unsigend int

unsigned int ts_base = millis();

on an arduino uno an unsigned int can store values between 0 and 65535
if millis() has grown to a bigger value than 65535 the if-condition for timing goes crazy

whenever using any kind of variable related to millis() you should define them as unsigned long

the function millis() returns unsigned longs
unsigned long can store values up to 4.294.967.295
as soon as millis() has values greater than 65535 the if-condition
in combination with having define ts_base as unsigned int

  if (millis() - ts_base >= interval_duration && PWokay == true) {

is true all the time and this is the reason why your IO-pin is switching at 4,7 kHz

You simply define all variables related to timing as unsigned long and it will work

const unsigned long t_int = 100;
unsigned long ts_base;// = millis();
unsigned long interval_duration = 0;

as an additional remark:
any code does always³ what you have coded. If the behaviour of your code is different than you expected you do not yet understand what you have coded.

³ except the one in a billion cases that the microcontroller has a manufacturing fault or an x-ray-burst out of space hit the microcontroller.

best regards Stefan

Thanks for your reply. Your suggestion indeed solved the issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.