3
\$\begingroup\$

I am working on a simple Arduino project which counts rotation using rotary encoder. After some calculation, I store this data to EEPROM. I am using an Arduino Nano.

The rudimentary block circuit diagram of the project is shown below:

enter image description here

The entire system is working completely fine when power supply is on, but when the power supply is disconnected, the Arduino is detecting some random pulse on the rotary encoder sensor pins and modifies the count and stores it in the EEPROM.

I have very little knowledge on circuit design, but I suspect this undefined behavior is because of the 1000uF capacitor.

Please suggest a circuit design to eliminate this undefined behavior.

\$\endgroup\$
9
  • 2
    \$\begingroup\$ Brown-out detection is required. Look it up. \$\endgroup\$
    – Andy aka
    Commented Aug 2, 2020 at 9:02
  • \$\begingroup\$ AVRs used to have a known issue with EEPROM corruption. It really needs proper reset if power supplies are not within specs for running. One solution was to not use the first page of EEPROM, and keep the write address register pointing to address 0 when not being used. \$\endgroup\$
    – Justme
    Commented Aug 2, 2020 at 10:09
  • \$\begingroup\$ How many times has the EEPROM been written? \$\endgroup\$ Commented Aug 2, 2020 at 10:34
  • 1
    \$\begingroup\$ Maybe rig up a voltage divider and monitor the regulator input, force a shutdown when it starts to fall. \$\endgroup\$ Commented Aug 2, 2020 at 10:50
  • 1
    \$\begingroup\$ put an opto on the 12v rail with a big enough resistor that it normally barely turns on, read the output with your sketch, don't save if opto output is off. \$\endgroup\$
    – dandavis
    Commented Aug 3, 2020 at 8:04

2 Answers 2

1
\$\begingroup\$

Use pull down resistors on the Arduino input pins assuming the output of the encoder is logic high (5v or 3.3v).

\$\endgroup\$
1
\$\begingroup\$

Supplied with 12V AC, the DC output of your rectifier/capacitor arrangement should be about 15V or so. The 5V regulator will begin to drop out when its DC input falls below 8V, so you have plenty of opportunity to warn the Arduino that DC input is falling.

Let's say that a "safe" input potential is 12V DC or greater. We just need a circuit to produce a 5V signal when that DC level exceeds 12V, and 0V otherwise, that you can connect to a digital input. Here's how you could do that using a PNP bipolar junction transistor, with a few peripheral components, in the dotted box:

schematic

simulate this circuit – Schematic created using CircuitLab

Node IN is connected to your rectified source of DC. Zener diode D1 begins to conduct as \$V_{IN}\$ exceeds +11V, preventing Q1's base from rising beyond that limit. As \$V_{IN}\$ continues to rise to +11.7V and beyond, Q1's \$V_{BE}\$ reaches 0.7V, switching it on, and pulling PG (power good) high. D2 clamps PG to a maximum of +4.7V, to be compatible with an Arduino digital input.

Here's PG potential as DC input voltage \$V_{IN}\$ is swept from zero to +15V:

enter image description here

Your Arduino software, prior to writing data to the EEPROM, should check that this signal is high, indicating that DC is still at +11.7V or more. Even if AC power is gone, you still have many milliseconds before the big DC reservoir capacitor discharges to an "unsafe" level.

Alternatively, configure the Arduino input to generate an "interrupt on change", and write an interrupt service routine to quickly write encoder position to the EEPROM in the few milliseconds remaining before \$V_{OUT}\$ drops below 5V, and inhibit further writes thereafter.

This design does rely on there being not more than a couple of volts or so of ripple on the DC source, which is highly dependent on the current being drawn from it. If ripple is too significant, you'll need to increase reservoir capacitance.

\$\endgroup\$
2
  • \$\begingroup\$ In addition: When the PG indicates a loss of power, stop reading/processing the signals from the rotary encoder. Those might also not be reliable anymore. \$\endgroup\$ Commented Dec 5 at 14:40
  • \$\begingroup\$ @BartvanIngenSchenau Perhaps, but PG doesn't only indicate a loss of power, it indicates an impending loss of power, and you do have some time before 5V starts to drop. \$\endgroup\$ Commented Dec 5 at 14:52

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.