Using the Sun to Keep Your Clunker Happy

Ari Mahpour
|  Created: March 1, 2022  |  Updated: November 26, 2023
Using the Sun to Keep Your Clunker Happy

A lot of folks have transitioned to working from home either part time or completely which means they sometimes leave their automobiles in the driveway for days or weeks at a time. I, for one, am definitely in this camp. I could easily go for weeks without starting my clunker which means, very frequently, my car battery will die. We usually combat this problem by periodically turning on our cars and drive around the block a few times. At the best of times this can be inconvenient but what tends to happen is that we forget and our battery has completely died. So how can we solve this problem? Ah yes, luckily for us there is the best power source known to man that is free and widely available. Solar panels have been cheap and easy to acquire and harnessing our most beloved power source, the sun, enables us to create energy anywhere for free. In this article we’re going to look at a solution to keep your car charged by the power of the sun and monitor it using one of our favorite IoT devices and platforms.

The Setup

For this project there are many different ways to skin the cat. The solution I propose here was the simplest at the time and enabled me to prototype it quickly with the parts I had around my house. This, by no means, is a one size fits all but this setup will definitely get you started and acquainted with a project of this type.

There are few things that we’ll need as a bare minimum to get started:

  1. A Solar Panel: 20W Solar Panel
  2. Battery Charging Device: Battery Tender - Solar Edition
  3. Jumper Cables
  4. Wire: 18 AWG Red/Black will suffice (or something else if a longer length is needed)

To monitor the charging over the internet you’ll also need the following as well:

  1. Internet Connected Microprocessor: NodeMCU board (see this getting started guide).
  2. Resistors: A set like this one is always good to have on hand
  3. Power source: A 9V Battery Holder + 9V Battery to power your NodeMCU board

Putting it Together

This project is really a two step process. The first is to set up the solar charger itself and the second is to create the IoT capabilities to monitor the charging circuit. Between the time I actually started this process and now there are already folks on the market that have put the car charging package together and sell it online. You could opt to skip the first part of the project and buy the charging kit off the shelf but what’s the fun in that?

First get your solar panel together and put it in a place where it will be exposed to the sun the majority of the day. I built a fixture that sits on top of my fencepost and tilted it in the direction where it could collect the most sunlight. I then ran some 18 AWG wires across to a tree and then down into the street next to my car. If you can achieve this all within your property I highly recommend it. I wasn’t able to so hopefully my neighbors won’t complain after seeing this article.

Figure 1: Solar panel setup with wiring
Figure 1: Solar panel setup with wiring

Once we have that set up we’ll want to grab a multimeter and confirm that the open circuit voltage matches the specification. The 20W Solar Panel that I suggested should be sitting at around 20V with no load in full sunlight. Take your extra wire that you purchased, connect it to your solar panel output (via crimping, soldering, electrical tape, etc.) and run that wire across to where your car will be parked. Now connect that wire to the input side of the Battery Tender device:

Figure 2: Image of the Battery Tender device
Figure 2: Image of the Battery Tender device

We will now connect the output of the battery tender to the rings of the Jumper Cables. Connect the two oversized alligator clips to your battery just like you would when you normally jump your car. You should see the LED transition from flashing to solid. This means that the Battery Tender is now charging your vehicle’s battery.

Charging Monitor

No DIY project would be complete without an IoT monitor. In this case we want to look at the battery voltage to monitor the state of charge. To do this we’ll need a few parts:

  1. NodeMCU board with MQTT set up.
  2. 10K and 1k Ohm resistors (for a simple voltage divider by a factor of ~10)
  3. Wire (to connect the battery tended to the voltage divider)

This step is fairly straightforward. What we’re going to do is stream the battery voltage from our car to the Adafruit IO MQTT server. Since the Analog input of the NodeMCU maxes out at ~3.3V we need a voltage divider to step down that battery voltage. Using a 10k and 1k Ohm resistor actually divides down the voltage by 11 (which is why I stated “~10” earlier) but it can be easily reversed in code so we get a “true” output from our MQTT stream. Connect up the resistors like so:

Figure 3: Circuit Diagram of IoT device
Figure 3: Circuit Diagram of IoT device

To minimize complexity we’re going to power the NodeMCU device with a 9V battery. Power can be picked off the 20W solar panel but requires more complex circuitry that is out of the scope of this article. Once we have all of that hooked up we can load up our NodeMCU with the following code:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "<Your SSID>"
#define WLAN_PASS       "<Your SSID Password>"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "<Your Adafruit IO username>"
#define AIO_KEY         "<Your Adafruit IO key>"

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'carbattery' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish carbattery = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/carbattery");

/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  // Initialize Serial monitor
  Serial.begin(115200);
  delay(10);

  // Initialize Analog monitor
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output

  Serial.println(F("Adafruit MQTT Car battery monitor"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
}

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // Read the voltage off the car battery
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - ~3.3V):
  float voltage_divided = sensorValue * (3.18 / 1023.0) - 0.05;
  // Reverse the voltage divider
  float voltage = voltage_divided * 11;

  // Publish the car battery voltage
  Serial.print(F("\nSending car battery voltage value "));
  Serial.print(voltage);
  Serial.print("...");
  if (! carbattery.publish(voltage)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }

  delay(10000);
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

Figure 4: Example code needed to run the NodeMCU device

Once everything is connected (including the connection to the car battery) you should now be able to see the voltage of your car battery streaming to your Adafruit IO MQTT server. Depending on what the voltage is (i.e. increasing, decreasing, or staying at the designated “charged” state) you should be able to tell whether your device is charging the battery or not. You can navigate to your MQTT feed and observe the battery voltage to look something like the image below.

Figure 5: Example graph of MQTT streamed battery voltage
Figure 5: Example graph of MQTT streamed battery voltage

Your final setup hooked up to the car should look something like this:

Figure 6: Complete setup of project
Figure 6: Complete setup of project

Conclusion

In this article we explored the idea of charging your car battery using a solar panel and specialized charging device for car batteries. In addition to that we also grabbed a NodeMCU and hooked it up to the car battery to monitor the battery’s voltage. By monitoring the battery voltage we can determine if it is charging (i.e. increased in voltage over time), discharging (i.e. decrease in voltage over time), or fully charged (i.e. voltage does not change over time). Now that you have the basics you can move on to designing a DC-DC converter to power your NodeMCU device.

About Author

About Author

Ari is an engineer with broad experience in designing, manufacturing, testing, and integrating electrical, mechanical, and software systems. He is passionate about bringing design, verification, and test engineers together to work as a cohesive unit.

Related Resources

Back to Home
Thank you, you are now subscribed to updates.