My sketch only runs while arduino is connected to the USB PC port

Hello everyone.
I've created an sketch to send the number "1" when a water level sensor detects theres no water and send the number "2" when the contrary using an arduino nano with those RF433MHz cards. Something happens thah, if I connect my project to the USB of my laptop, it works fine. If, for example, I try to connect using a USB 5V power supply, it won't work. So, I changed the nano for a Mega and it is exactly the same situation: works fine with the computer USB, it doesn't with a 9V power supply or a 5V USB power supply. here I let you the code:

#include <RH_ASK.h>
#include <SPI.h>


RH_ASK rf_sistema(2000);   //for the radio device

int sensor;                          //the variables were write in spanish, sorry.
int sumatorio;
int contador;
int nivel;


void setup(){
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  rf_sistema.init();
  Serial.println("initializing system");
  sumatorio = 0;
  contador = 0;
  nivel = 0;
}

void loop(){
  do{
    sensor = analogRead(A1);
    sumatorio += sensor;
    contador += 1;
    delay(500);
  }while(contador <= 15);
  nivel = sumatorio / 15; 
  Serial.println("First reading");
  Serial.print("average level ");
  Serial.println(nivel);
  if(nivel <= 200){
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("Low level of water detected ❌");
    Serial.println("⛔ Starting re-filling process... ⛔");
    sumatorio = 0;
    contador = 0;
    for(int i = 0; i < 100; i++){
      const char *msg = "A";
      rf_sistema.send((uint8_t *)msg, strlen(msg));
      rf_sistema.waitPacketSent();
      Serial.print(".");
    }
    Serial.println();
    Serial.println("End of transmittion");
    do{
      sensor = analogRead(A1);
      Serial.print("Current level: ");
      Serial.println(sensor);
      delay(1000);
    }while(sensor <= 200);

  }
  else{
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("Right level of water detected ✅");
    Serial.println("⛔ Re-filling process finished. ⛔");
    for(int j = 0; j < 100; j++){
      const char *msg = "B";
      rf_sistema.send((uint8_t *)msg, strlen(msg));
      rf_sistema.waitPacketSent();
      Serial.print(".");
    }
    Serial.println();
    Serial.println("End of transmittion");
    do{
      sensor = analogRead(A1);
      Serial.print("Current level: ");
      Serial.println(sensor);
      delay(1000);
    }while(sensor > 200);
  }
  sumatorio = 0;
  contador = 0;
  nivel = 0;
}

I thank you in advance for your help.

Hi, Welcome...

I didn't analyze your code as others are much better at this than I.

However my "standard" troubleshooting process for issues such as this is:

  1. Disconnect everything from the processor (i.e. you radio device etc).
  2. Load the example program "Blink without Delay.ino"

Test for function with USB and external power supply.

Your results will determine the next step.

Could you provide a bit more info on the power supplies being used that don't work?

Such as is the USB supply a charger?

Is the 9V supply a battery?

A image of the power sources would be nice to see.

Are you putting 5V into Vin?

1 Like

Thanks for your response.

I solved the problem changing the transmitting port from the default 12 to number 10 by using this:

RH_ASK rf_sistema(2000,11,10);

but know I don't know why but the receiver is not receiving. I'm doing more troubleshooting; if I solve the situation, I'll let you know.

Thanks for your response.

regarding the power supply I was using a generic 5V 2A. power supply with USB connector, one I use to charge my phone.

the 9V 3A. one is another generic one. no batteries right now

No, I wasn't using the Vin.

I realized that the problem was the D12. I change it for Pin 10 and it started to work, but know it is not transmitting. Maybe I forgiot to change some cable, or it is something else.
if I solve the situation, I'll let you know.

If you connected the 9V you probably fried it. Try another radio if you have one.

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