Arduino nano loses program?

Hi All,
A bit of a strange one.
I have an arduino nano (with CH340G, bought on ebay ) running an rgb program controlling rgb led panels through mosfets.

I have had zero issues until today. Today was the first time I ran it for more than a few minutes. It was powered on the whole day with the red colour potentiometer on only, i.e. red leds on. Tonight I turned it off to do some experiments with the placement of the panels, and when I turned it back on, all leds were powered on. The three potentiometers had no effect whatsoever. Brilliant white light, but no control.

So, I connect via USB, re-upload the sketch, and turn it on again. All good, at least for a few minutes. A few restarts later (in the space of a couple minutes) and same problem.

Any ideas? Its not the sketch, because that runs just fine. Power isn't an issue either. I'm using the 5v line from an ATX psu.

What's going on?

Which pin is the ATX psu connected to?

Its not the sketch, because that runs just fine.

Not very certain... you really don't know that yet. Sketches can have different short term and long term behaviour for a variety of reasons. You should probably post it in code tags.

Thanks for your quick reply. Its connected to Vin and Gnd.

int redPin = 9; //Pin for the red RGB led pin
int greenPin = 10; //Pin for the green RGB led pin
int bluePin = 11; //Pin for the blue RGB led pin
 
int potPin_red = A0;  //declare pin for the potentiometer for the red LED
int potPin_green = A1;  //declare pin for the potentiometer for the green LED
int potPin_blue = A2;  //declare pin for the potentiometer for the blue LED
 
int readValue_red; //declare variable to store the read value from the potentiometer which controls the red LED
int readValue_green; //declare variable to store the read value from the potentiometer which controls the green LED
int readValue_blue; //declare variable to store the read value from the potentiometer which controls the blue LED
 
int writeValue_red; //declare variable to send to the red LED
int writeValue_green; //declare variable to send to the green LED
int writeValue_blue; //declare variable to send to the blue LED
 
void setup() {
  pinMode(potPin_red, INPUT); //set potentiometer for red LED as input
  pinMode(potPin_green, INPUT); //set potentiometer for green LED as input
  pinMode(potPin_blue, INPUT); //set potentiometer for blue LED as input
  
  pinMode(redPin,OUTPUT); //set pin for red LED as output
  pinMode(bluePin,OUTPUT); //set pin for green LED as output
  pinMode(greenPin, OUTPUT); //set pin for blue LED as output
}
 
void loop() {
  readValue_red = analogRead(potPin_red); //Read voltage from potentiometer to control red LED
  readValue_green = analogRead(potPin_green); //Read voltage from potentiometer to control green LED
  readValue_blue = analogRead(potPin_blue); //Read voltage from potentiometer to control blue LED
  
  writeValue_red = (255./1023.)*(1023.-readValue_red); //Calculate the value to write on the red LED (add point to change to float point)
  writeValue_green = (255./1023.)*(1023.-readValue_green); //Calculate the value to write on the green LED
  writeValue_blue = (255./1023.)*(1023-readValue_blue); ///Calculate the value to write on the blue LED
  
  analogWrite(redPin,writeValue_red); //write value to set the brightness of the red LED
  analogWrite(greenPin,writeValue_green); //write value to set the brightness of the green LED
  analogWrite(bluePin,writeValue_blue); //write alue to set the brightness of the blue LED

nealieboyee:
Thanks for your quick reply. Its connected to Vin and Gnd.

Well, that is wrong.

So if it's a well-regulated 5V supply, I can connect to the +5V pin directly?
Is 5V not enough to run through the regulators?

nealieboyee:
So if it's a well-regulated 5V supply, I can connect to the +5V pin directly?
Is 5V not enough to run through the regulators?

Yes and yes.

Awesome, thanks for your help. Any idea why that would cause my problem? Not enough juice to start it up correctly or something?

nealieboyee:
Awesome, thanks for your help. Any idea why that would cause my problem? Not enough juice to start it up correctly or something?

VCC too low and/or overheating of the onboard regulator.

Still the same problem. I moved the 5v supply to the +5V pin and uploaded the sketch again. It worked. As soon as I cycled power, all Leds came on full (white). Potentiometers have no effect. Repeatedly cycling power has no effect either.

V-in (raw) is the input of the 5volt regulator.
It needs at least 6volt for the regulator to make a stable 5volt for the board.
If you have a reliable 5volt, then connect it directly to the 5volt pin.

Mighty big chunk of code for a simple thing, and why use floats.

Compressed version attached. Only difference is that the pots now work normally (not reversed).
Leo..

const byte redPin = 9;
const byte greenPin = 10;
const byte bluePin = 11;
const byte potPin_red = A0;
const byte potPin_green = A1;
const byte potPin_blue = A2;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  analogWrite(redPin, analogRead(potPin_green) >> 2);
  analogWrite(greenPin, analogRead(potPin_red) >> 2);
  analogWrite(bluePin, analogRead(potPin_blue) >> 2);
}

Post a diagram/pictures, and links to the LED panels and mosfets.
How much current do the LEDs draw, and did the mosfets get hot (with ~4volt drive).
You might already have blown the mosfets.
Leo..

Thanks Leo,
I'll give that code a try.
Mosfets are IRL540. They don't even break a sweat. Each panel is diy and draws 0.6A with all colours fully on. Total draw is 1.8A for all three panels.

Mosfets don't even get warm.

I don't get it though. It was working beautifully until I restarted it today a couple of times in quick succession, and it works after I reupload the code. Until a few restarts later, that is.

Corrected a stupid mistake (should have tested the code).
Please reload.
Leo..

Thanks Leo. I've run the updated code and it works.....backwards. Pots work the wrong way around.

The code seems to be stable at the moment. After a few restarts, its still holding the code.

nealieboyee:
I've run the updated code and it works.....backwards. Pots work the wrong way around.

My code increases brightness with a higher voltage on the pot, your code does the opposite.
That makes YOUR code backwards :slight_smile:
Just swap the two outer wires on the pot.
Leo..

Lol. Thanks. Is there any way I could swap it in the code? My pots and board are all mounted and cable tied, soldered etc. If not, no worries.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Look at how your map function is constructed, compare your code with the new code

Thanks. Tom.. :slight_smile:

Oh is something wrong Tom? Or just for ideas?

Hi,

Did the new code fix your problem?

I have advised this to help with hardware solutions.

Look up "Blink without Delay" in the IDE Examples and put that in your code.

It will make your on board LED blink, if it stops then you know your code is not running

Did you write the code, if so you should be able to work out the direction problem with your code.

Tom..... :slight_smile:

Oh I see. No, I didn't write the original code, but I modified it to reverse the pots direction (I added 1023- to the writeValue lines. It did the job.

Looking at the new code, I'm trying to understand how a shift right works with an analogue input. Baffling...

The problem seems to be gone, but I still don't understand what caused it. If it was working before, why suddenly act up on a few restarts? Doesn't make sense.

I'll add the blink without delay code to help me determine if code is running or not.

This is my circuit, but add in your pots on A0, A1, and A2. Wiper to analog inputs, and then one side each to +5V and Gnd.