Know-How
Completed
Digital input not reading: NPN or PNP, step by step
248 Views •4-20 mA input reading wrong: check it step by step
238 Views •RS-485: how to check a bus step by step
234 Views •Scaling a 4-20 mA signal in the PLC: formula, constants and code
229 Views •Relay or transistor output: which one and how to check it
189 Views •Modbus RTU timeout: 6 checks before blaming the wiring
142 Views •Upload fails: 6 checks before blaming the code
138 Views •4G PLC not connecting: 6 checks before blaming the code
105 Views •LoRa works on the desk, fails in the field: 6 checks before you install
94 Views •Choosing a PLC 1: count the I/O and leave spare
32 Views •Choosing a PLC 2: which communications do you really need
32 Views •SD card corrupted? 6 habits for Linux in the field
30 Views •LoRaWAN node won't join: 6 checks, in order
30 Views •Touch panel: screen turned, touch didn't. 6 checks
30 Views •Arduino IDE compile error: 6 checks before the code
29 Views •SD card corrupted? 6 habits for Linux in the field
Short answer: an SD card that gets corrupted in a Linux device is often the result of a power cut during a write, not only of a faulty card. Let writes finish, keep ext4 journaling, shut down from software, plan for outages with a shutdown signal, write less and protect the system with a read-only root and a backup image.
Download the checklist (PDF, 2 pages)
Descarga la lista de comprobación (PDF, 2 páginas)
These habits apply to any Linux device that boots from an SD card or eMMC: Raspberry Pi based PLCs and panel PCs, gateways and edge computers. Cards also fail from wear or poor quality, so look at both causes before you replace one.
Six habits, in this order
| Habit | What to do | Good result |
|---|---|---|
| 1. Writes | Applications write to the RAM cache first and the kernel moves the data to the flash later. ext4 commits data and metadata every 5 seconds by default (the commit mount option). If the power goes in between, that data can be lost. Applications that must not lose a record call fsync after writing it. | Critical data is on the flash before the application moves on. |
| 2. File system | Keep ext4 with its journal and the default data=ordered mode, where data is written before the metadata is committed to the journal. Journaling keeps the structure of the file system consistent; it does not guarantee the content of a file that was being written. It also relies on the card writing in order, so it cannot cover a failure inside the card controller. | The file system mounts after a cut; the files that were open are checked. |
| 3. Shutdown | Shut down from software before you remove power: sudo poweroff or the Shutdown option of the desktop. A power button wired to an input that triggers the shutdown (on Raspberry Pi, the gpio-shutdown overlay) tells the operating system; a switch that cuts the supply does not. | The system is off before the supply is cut. |
| 4. Outages | A UPS or a supercapacitor helps when it tells the system to shut down in time. With Network UPS Tools, upsmon runs the shutdown command when the UPS reports on battery and low battery. Some backup boards for Raspberry Pi signal the power loss on a GPIO input instead. Without a signal, the backup only delays the cut. | Every outage ends in a clean shutdown. |
| 5. Write less | The flash in SD cards has a limited write cycle lifetime. Keep the systemd journal in RAM with Storage=volatile, put temporary files on tmpfs and turn off debug logging to the card. Logs in RAM are lost at reboot: send the ones you need for diagnosis off the device. | Fewer writes to the flash, slower wear. |
| 6. Protect | Mount the root read-only, or enable the overlay file system in raspi-config: writes go to RAM and are lost at reboot. Choose cards from a reliable brand, industrial grade (power fail protection, extended temperature range) or eMMC if the site needs it; eMMC is not immune either. Keep a tested backup image and the data off the device. | Recovery is a reflash, not a rebuild. |
What protects what
- Journaling: the structure of the file system, not the file being written.
- Software shutdown: the writes still in the cache.
- UPS or supercapacitor with a signal: power cuts.
- Fewer writes: flash wear.
- Read-only or overlay root: the operating system.
- Backup image: the time to recover.
Reference commands
Shut down: sudo poweroff. Logs in RAM: Storage=volatile in the [Journal] section of /etc/systemd/journald.conf. Temporary files in RAM: a line such as tmpfs /tmp tmpfs defaults,noatime,nosuid 0 0 in /etc/fstab. Overlay: sudo raspi-config > Performance Options > Overlay File System. Logs in RAM and changes on the overlay are lost at reboot: keep the data you need on a separate partition or on another device.
Next steps
- Touch panel: the screen turned, the touch didn't. 6 checks
- Free course: First Steps with Raspberry Pi PLC
- How to set up the UPS of a Raspberry based PLC (controlled shutdown)
- Raspberry PLC: industrial PLC with Raspberry Pi and Linux
Sources
Raspberry Pi Ltd: Making a more resilient file system (white paper RP-003610-WP). Raspberry Pi documentation: Getting started (Shutdown options) and Configuration (raspi-config). Linux kernel documentation: ext4 and tmpfs. systemd: journald.conf(5). Linux man-pages: fsync(2). Network UPS Tools: upsmon(8).
Commenting is not enabled on this course.