-
Lashing it all together.
12/16/2015 at 21:38 • 0 commentsI 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 ... http://stm32duino.com/viewtopic.php?f=45&t=745&start=50 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> ADC_MODE(ADC_VCC); const char* ssid = "yourssid"; const char* password = "yourpassword"; int relayPin = 4; //GPIO4 int ledPin = 2; // GPIO2 WiFiServer server(80); void setup() { Serial.begin(115200); delay(10); ESP.getVcc(); // 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.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.println("/"); } void loop() { // Check if a client has connected WiFiClient client = server.available(); if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while (!client.available()) { delay(1); } // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); // Match the request int value = digitalRead(ledPin); if (request.indexOf("/LED=ON") != -1) { digitalWrite(ledPin, LOW); value = LOW; toggleRelay(); } if (request.indexOf("/LED=OFF") != -1) { digitalWrite(ledPin, HIGH); value = HIGH; toggleRelay(); } // 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.println("<html>"); client.print("Led pin is now: "); if (value == LOW) { client.print("On"); } else { client.print("Off"); } client.println("<br></br>"); client.print("Battery Voltage: "); int batteryVoltage = ESP.getVcc(); client.print(batteryVoltage / 1000); client.print("."); client.println(batteryVoltage % 1000); client.println("<br></br>"); 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>"); client.println("</html>"); delay(1); Serial.println("Client disonnected"); Serial.println(""); } void toggleRelay() { digitalWrite(relayPin, LOW); delay(300); digitalWrite(relayPin, HIGH); delay(300); digitalWrite(relayPin, LOW); delay(300); 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.