How To Prevent SD Card From Getting Corrupted (Best Practices)
Working with an SD card and a Raspberry Pi can be tricky for beginners, especially since you don’t know all the precautions you need to take in order to ensure that your Raspberry Pi and SD card can last a long time. One major thing you need to watch out for is corrupting your SD card. Consequently, you may be wondering: “How do you prevent an SD card from getting corrupted?”
Your SD card may become corrupted when working with Raspberry Pi due to two factors: overclocking and power loss. Consequently, the best practices you need to follow to prevent these factors from happening are properly shutting down your Raspberry Pi and keeping your Raspberry Pi at its original speed of 1.5 GHz (gigahertz).
Another reason why your SD card may become corrupt has nothing to do with your Raspberry Pi: perhaps you purchased a bad quality SD card or the SD card arrived with a manufacturing defect.
For the rest of the article, I’ll go over what SD card corruption is, how you can properly shut down your Raspberry Pi, how you can check if your Raspberry Pi is overclocked, and what you can do if your SD card is corrupted.
What Is SD Card Corruption?
A corrupt SD card means that you wouldn’t be able to access files from it. Moreover, your Raspberry Pi might not recognize it, the files might disappear, or your SD card won’t even open.
Some common causes of corrupt SD cards are malware, physical damage, mishandling, manufacturing defects, and more.
Moreover, you can easily corrupt the SD card if you shut down your Raspberry Pi while the Raspberry Pi is in the middle of writing to the card.
The main thing you can do to reduce your chances of having your SD card get corrupted is by learning:
How To Properly Turn Off Your Raspberry Pi
To power off your Raspberry Pi, you can use one of the five following commands:
sudo halt
This command will halt your Raspberry Pi
sudo poweroff
As you can see, this command will power off your Raspberry Pi
sudo shutdown now
This command will shutdown your Raspberry Pi the moment you press enter
sudo shutdown
This command will shutdown your Raspberry Pi after 1 minute
sudo shutdown 05:00
This command will shutdown your Raspberry Pi at 5 am. You can modify it by changing the “05:00”. Remember, the terminal follows the 24-hour clock system, so if you wanted to shutdown down your Raspberry pi at 11 pm, you’ll need to replace “05:00” with “23:00”.
As an added bonus, if you’re in the midst of shutting down your Raspberry and changed your mind, you can use this command
sudo shutdown -c
This command will cancel the shutdown
Now, if you don’t feel like memorizing these commands or if you don’t like using the terminal, you can directly use the Raspberry Pi’s Desktop GUI (Graphical User Interface). Here are the 3 simple steps to help you get started:
- Open the applications menu (click on the Raspberry Pi icon on the top left corner of your screen)
- Click on Logout
- Finally, click on Shutdown
Note: The other two options are Reboot and Logout. Reboot basically shuts down your Raspberry Pi and turns it on again. Logout will sign you out of your account and another user can sign in.
How To Check Your Raspberry Pi Clock Speed
Overclocking is when you’re setting a higher speed for your Raspberry Pi to make it run faster. You could get a lot of work done by doing this, but it leaves your Raspberry Pi vulnerable.
Consequently, I will show you 3 commands to check your clock speed and make sure it’s not being overclocked.
1. Current Clock Speed
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
This command will display your current clock speed in terms of hertz. If your Raspberry Pi isn’t overclocking, then the number you see should be less than 15,000,000,000 hertz.
2. Maximum CPU Speed
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
This command should tell you the maximum speed the Raspberry Pi would run at if it was under load. This number should be greater than the minimum CPU speed.
3. Minimum CPU Speed
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
This command will tell you the CPU speed of your Raspberry Pi if it was idle (meaning that it’s doing nothing at the moment).
What To Do If SD Card Gets Corrupted
If you find yourself with a corrupt SD card, the best thing you can do is to recover the data. I don’t recommend going back and trying to reuse the SD card because each time you try to do that, the chances of recovering the data will decrease.
To recover the data, you can use a professional SD card recovery software like Disk Drill.
After that, you can try to recover your SD card, especially if you’re on a tight budget. If not, then I’d recommend buying another SD card to save yourself the trouble of trying to fix it. On top of that, if this is the second time your SD card has been corrupted, then you should definitely replace it or else it will become a reoccurring issue.
Summary – tl;dr (Too Long; Didn’t Read)
Here are the key takeaway points you should keep in mind after reading this article:
- SD cards can be corrupted due to three factors: manufacturing defects, overclocking, and sudden power loss.
- Sudden power loss will corrupt your SD card if your Raspberry Pi is in the middle of writing to the SD card. Consequently, you should shut down your device using one of the following five commands:
- sudo halt
- sudo poweroff
- sudo shutdown
- sudo shutdown now
- sudo shutdown 05:00
- You can prevent overclocking by ensuring that your Raspberry Pi is running at its normal speeds. You can use one of the three commands to ensure that your Raspberry Pi falls under 15,000,000,000 hertz:
- cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
- This is the speed at which your device is currently running at
- cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
- This is the max speed your device can run at when it’s under load
- cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
- This is the min speed your device can run at when it’s idle
- cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
- Sudden power loss will corrupt your SD card if your Raspberry Pi is in the middle of writing to the SD card. Consequently, you should shut down your device using one of the following five commands:
- If your SD card is corrupted, the best thing you can do is to recover the data by using SD card recovery software
- You can try to save your SD card and reuse it, but it’d be better if you just bought another one.
In the end, I hope you learned a lot and disk-covered the best tips you can implement to prevent your SD card from getting corrupted!