Lashing it all together.

A project log for Magic smoke enabled 18650 LiPo battery pack.

More mildly perilous fun with recovered 18650 LiPo cells and cheap Chinese goods.

andyhullandyhull 12/16/2015 at 21:380 Comments

I did a little more work with this over my lunch hour today, I added an ESP8266 to the mix to let me test the idea of switching on and off the "torch output".

See ... for a full run discussion of what was involved.

I took an existing arduino ESP8266 sketch that allows me to toggle the LED on GPIO Pin 2 using a web page on the ESP8266 and modified the sketch to send a couple of pulses to the battery pack to switch on the "Torch LED" MOSFET. I then lashed up a 3V relay with a snubber diode and some flying leads and attached the coil in line with the +Vbat and the Torch Out MOSFET. Switching on GPIO4 turns on the MOSFET which acts as a low side switch and switches on the relay.

I attached the relevant pins to the wires I had previously run out of the battery pack. The big mess 'O wires in below is the result. (The wires exiting from the bottom of the picture go to my USB to serial converter used for programming).

When I visit the web page, now as well as toggling the LED on pin2 the ESP8266 code sends a couple of pulses via GPIO4 on the ESP8266 to the button pin on the battery controller via a 2K resistor. Result.... I get a very satisfying click from the relay, as it latches on or off, depending on its previous state.

The ESP8266 GPIO cannot drive a 50mA relay (they are limited to 12mA source, 20mA sink), but the "Torch LED output" pin on the battery pack can.

A sequence of 300ms LOW, 300ms HIGH, 300ms LOW and back to HIGH seems to be the sweet spot to make it work 100% of the time.

#include <ESP8266WiFi.h>
const char* ssid = "yourssid";
const char* password = "yourpassword";

int relayPin = 4; //GPIO4
int ledPin = 2; // GPIO2
WiFiServer server(80);

void setup() {
  // Start up, switch on the LED and set the relay toggle pin HIGH
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);

  // Connect to WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");

  // Start the server
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL to connect: ");


void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {

  // Read the first line of the request
  String request = client.readStringUntil('\r');

  // Match the request

  int value = digitalRead(ledPin);
  if (request.indexOf("/LED=ON") != -1)  {
    digitalWrite(ledPin, LOW);
    value = LOW;
  if (request.indexOf("/LED=OFF") != -1)  {
    digitalWrite(ledPin, HIGH);
    value = HIGH;

  // Set ledPin according to the request
  //digitalWrite(ledPin, value);

  // Return the response
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println(""); //  do not forget this one
  client.println("<!DOCTYPE HTML>");

  client.print("Led pin is now: ");

  if (value == LOW) {
  } else {
  client.print("Battery Voltage: ");
  int batteryVoltage = ESP.getVcc();
  client.print(batteryVoltage / 1000);
  client.println(batteryVoltage % 1000);
  client.println("Click <a href=\"/LED=ON\">here</a> turn the LED on pin 2 ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> turn the LED on pin 2 OFF<br>");

  Serial.println("Client disonnected");


void toggleRelay() {
  digitalWrite(relayPin, LOW);
  digitalWrite(relayPin, HIGH);
  digitalWrite(relayPin, LOW);
  digitalWrite(relayPin, HIGH);

Bear in mind, we are switching the low side here, so the current path for the relay is B+ > Relay coil > Battery pack MOSFETt > Gnd

There is nothing special about the relay, other than the fact that it has a 3V coil and the contacts are rated 3A. The relay could be used to switch pretty much anything that draws 3A or less, including a higher power relay, or another LiPo pack.

By the same token, you could use the battery pack MOSfet to switch any 3V source at up to 100mA, it doesn't have to switch a relay, it could switch a higher powered logic level MOSfet or BJT Transistor, a buzzer, or whatever takes your fancy, including another microcontroller, so long as it limits itself to 100mA or less.

Another option would be to power the ESP8266 via the relay, switch it on with the button on the power bank, and get it to switch itself off by toggling the pin. This is a little wasteful, as the relay draws almost as much as the ESP8266, but it has the advantage that once switched off, the ESP is completely isolated and its current draw is therefore zero.