I have a new phone and want the battery health to last as long as possible. Charging it to 100% overnight, every night, may reduce the battery capacity over time so I have been looking for a solution to help me to protect the battery.
Android does not offer a way to control the charging directly so I needed a solution external to the phone.
The Plan:
Build a solution to shut off the phone charger when it reaches a certain percentage by using a Raspberry Pi and a relay.
You can see the final board layout in a simple case on the second tab. Note that this is an original Raspberry Pi 1B, but the solution is likely to work with any Pi.
Bill of Materials:
- Raspberry Pi (I used a 1B but any Pi should do, or even a Pico!)
- 5V Single Channel Relay Module 10A
- 5V 2A DC Power Adapter
- USB Cable (to solder)
- 10 Watt USB Powered Qi Charging Pad (mine came from officeworks)
Additional Hardware:
- An Android Phone (this guide uses an app called automate, I'm unsure of an iOS equivalent)
- Soldering Iron
Details:
The solution works in two parts.
Firstly, I created a script that uses the Android Automate app ( a free app from llamalab).
The script performs the following steps:
- Check if I am connected to my home WiFi
- Check if the phone is currently being charged
- If the battery level is over 85%, send an HTTP request to my Raspberry Pi
Secondly, I created a tiny web server running on a Raspberry Pi.
If this web server receives any request, it switches a relay to turn off the power to the phone charger. This is such a small script it would likely work on a Raspberry Pi Pico too!
#!/usr/bin/python # Script to stop phone charging when an http request is received # To stop the phone charging a relay is connected to a raspberry pi GPIO pin # JKenyon 2024 # Libraries import RPi.GPIO as GPIO from time import sleep import socket # Define constants SERVER_HOST = '0.0.0.0' SERVER_PORT = 8001 RELAY_PIN = 24 SLEEP_TIME = 5*60*60 # 5 hours # Define a simple web server that quits after it receives a connection def waitForConnection(): # https://www.codementor.io/@joaojonesventura/building-a-basic-http-server-from-scratch-in-python-1cedkg0842 # Modified to exit when a connection is made # Create socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind((SERVER_HOST, SERVER_PORT)) server_socket.listen(1) # Wait until we receive any connection (as we don’t parse anything this is pretty safe) client_connection, client_address = server_socket.accept() response = 'HTTP/1.0 200 OK\n\n' client_connection.sendall(response.encode()) client_connection.close() # Close socket server_socket.close() # Define a function to control the GPIO and switch off the charger def switchOffChargerForTime(delay): GPIO.setmode(GPIO.BCM) GPIO.setup(RELAY_PIN, 0) sleep(delay) GPIO.setup(RELAY_PIN, 1) GPIO.cleanup() while True: waitForConnection() switchOffChargerForTime(SLEEP_TIME)
The GPIO code is not necessarily the perfect way to control a relay. I chose to do it this way as the relay I have was not switching reliably at 3.3V. If you have a relay that is better behaved then you can change the power to the relay module to pin 1 (to supply 3.3V) and use GPIO.output(RELAY_PIN, GPIO.HIGH) and GPIO.output(RELAY_PIN, GPIO.LOW) to switch the relay.
The diagram in the second tab shows how everything is connected. I had originally planned to use the Raspberry Pi to supply the 5V to the charger, but this was a bit too much so I settled on using a separate power supply.