I have a problem with my homemade fan speed controller powered by an Arduino Nano, which reads a value from Serial (Arduino serial monitor, or Processing sketch) and uses the 0~255 value to set a variable fanspeed1 to set a PWM pin to control the fan speed.

The problem is when I close the Processing sketch, or the Arduino serial monitor, fanspeed1 is changed to max value (the fan speeds up to full speed).

I would like to send serial to set the value and then close the serial monitor, or Processing sketch and the value of fanspeed1 should not change.

If I need to post some code I can simplify my sketch down to a send serial, receive serial, make pin pwm = serial(0~255), but I am guessing there is a simple solution to this unfindable with my googling.

Help please, these fans need controlling...

  • 1
    Showing us your simplified sketch code would be useful here. For now, try to protect 'fanspeed1' from changing when serial communication is not active.
    – smajli
    Commented Nov 13, 2017 at 10:40
  • 2
    Disable the auto-reset functionality of the board?
    – Majenko
    Commented Nov 13, 2017 at 11:07
  • It's 0~255, not 256.
    – dda
    Commented Dec 14, 2017 at 5:39

1 Answer 1


You could consider writing/reading the value to/from the EEPROM.


This allows the board to be reset, or even turned off, and retain the value previously set. What you would be doing is saving the value to the internal memory of the Arduino. This is resettable and persistent over a "reboot".

Granted, there is a limited number of writes you can do, but that number is high enough (~100k) that you will probably wear out the fan before the EEPROM sector dies.


EEPROM.write(address, value)


EEPROM.update(address, value)

For objects:

EEPROM.put(address, data)

EEPROM.get(address, data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.