One Button Restart of a Stalled Raspberry Pi Zero W

One Button Restart of a Stalled Raspberry Pi Zero W © MIT

Avoid SD card corruption on a non-responsive RPi using an Arduino to log into the serial console and restart your RPi from the root account.

  • 3,663 views
  • 0 comments
  • 19 respects

Components and supplies

About this project

Voltage

Please be aware that I am running the Arduino ProMicro at 3.3V, and 5V is recommended. I have not had any issues with the ProMicro 16MHz working properly, but please understand that if your ProMicro must be run at 5V to function you will absolutely be required to implement voltage level conversion for the Arduino Tx serial output. See this article (a Google search will get numerous results, too.)

convert-5-volts-to-3.3

Alternately, you could use the 3.3V ProMicro which runs at 8MHz but you will need to lower your default BAUD rate for the RPI Zero W to a lower speed; some testing is required.

I have been practicing the black-art of C++ programming

Needless to say, I have managed to cause some rather nasty situations whereby I have lost control of the RPi over SSH or VNC and even on an occasion managed to hose up the GUI session running on an HDMI monitor. What I found during my investigations is the Raspbian OS was still running and the hardware was still alive, but I had managed to trash a few threads and crash an interface or two! What I needed was a way to avoid corrupting the SD card; likely if I issued a hard-reboot, and likely if unplugging the wall wart.

Connectivity Solutions

I did some light research with Google and found a few creative connectivity implementations that have been previously published.

Representative published solutions:

  • SSH - Secure Shell
  • VNC - Virtual Network Computing
  • Physical Keyboard / PC + USB-Serial

Another creative shutdown via button.

First Attempt

Unfortunately, most solutions assume that the RPi is running properly and that SSH or VNC is available for use or that Python is available. My implementation takes the console login and tosses the PC and the USB-serial hardware: I use an Arduino to monitor a button state and then send the required commands from the Arduino to the RPi over the console interface, GPIO15/UART0-Rx.

I tried a barebones Arduino (just the atmega328P-PU) running at 8MHz from the internal RC oscillator - failure for a number of reasons, but the default BAUD of the RPi is 115200 and I did not want to change the default to work with a slower clocked AVR.

Second Attempt

My second attempt was with an atmega328P-PU barebones running at 16MHz using a high-quality crystal. This worked but required an AVR programmer to install the code and is not very convenient for changes. Still, it is the way to go for low-cost and minimum parts count.

I moved to an Arduino Mini, a cheap clone from China that is sold typically under $2. This worked, too, but required a FTDI (or clone) USB-serial adapter and just seemed a bit clumsy. I finally settled on a better Arduino, the Arduino Micro. Based on the atmega32U4, this clone board is just a $1 more than when ordered from China and has a nice USB-micro connector on the board. Now all I need is a USB cable to program the device should I wish to experiment more - and I do want to play more!

The Idea

The simple idea of having the Arduino log into the RPi Zero W to perform my bidding is intriguing. The Arduino has analog inputs, digital inputs, and will be directly connected to serial console and capable of logging in as any user.

Consider the possibilities:

  • Log in and post analog values
  • Monitor external sensors, perform logic, login and post values
  • Use multiple push buttons to perform system administration
  • Use multiple push buttons to kick-off RPi scripts or program code

So, when everything is distilled, we have a very powerful concept that is wide-open in possibilities. My thinking is to mount the Arduino and a lithium battery in a small plastic box for portability - connection would only require Gnd and the Arduino Tx which would connect to GPIO15. I am thinking about using a rotary encoder to map numeric to commands and having a nice repertoire of emergency commands available at the push of a button.

Code

Arduino ProMicro SketchC/C++
This is for a Sparkfun ProMicro or Arduino Micro using the 32U4 microcontroller. If another 8-bit AVR is used, please note you will need to change serial1 to serial.
Streaming library is available here: http://arduiniana.org/libraries/streaming/
/* Pro Micro Test Code
*/

  #include <Streaming.h>

  const byte  bootPin = 9;
  void setup()
  {
  pinMode(bootPin, INPUT);
  digitalWrite(bootPin, HIGH);    // enable pullup
  Serial1.begin(115200);          // This pipes to the serial TxRx
  Serial.begin(9600);             // USB serial, BAUD is just a placeholder
  Serial << "Initializing...\n\r";

  }

  void loop()
  {
  if (! digitalRead(bootPin)) {
    Serial1 << "\n\r";
    Serial << "\n\r";
    delay(1000);
    Serial << "Attempting to login as user Pi";
    Serial1 << "pi\n\r";
    delay(1000);
    Serial1 << "raspberry\n\r";
    delay(1000);
    Serial << "Attempting to write cache to SD\n\r";
    Serial1 << "sudo sync\n\r";
    delay(1000);
    Serial1 << "sudo sync\n\r";
    delay(1000);
    Serial << "Issuing the sudo reboot now command\n\r";
    Serial1 << "sudo reboot now\n\r";
    }
  }

Schematics

ProMicro
Sparkfun 8-bit 32U4-based board

Comments

Similar projects you might like

Arduino 101 Connects with Raspberry Pi Zero W

Project tutorial by Alexis Santiago Allende

  • 8,198 views
  • 0 comments
  • 17 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 28,266 views
  • 9 comments
  • 76 respects

Raspberry Pi - Powered Candy Dispenser

Project tutorial by Arduino “having11” Guy

  • 5,284 views
  • 1 comment
  • 12 respects

Home Automation Using Raspberry Pi 2 And Windows 10 IoT

Project tutorial by Anurag S. Vasanwala

  • 297,268 views
  • 98 comments
  • 706 respects

Interfacing Arduino with Raspberry Pi

by Sankar Cheppali

  • 84,837 views
  • 9 comments
  • 25 respects
Add projectSign up / Login