Introduction: From Flashlight to Motion Sensor With ESP8266 and MQTT

About: an environmental researcher, learning electronics, programming, and sensors by tinkering

In this post, I would present items below:

  • LEDs need a limiting current circuit
  • how to make a flashlight
  • make a light powered by the portable battery, and dimming the LEDs by ESP8266 via MQTT

The video is the recap and a short explanation of how this works,

I planned to have PIR sensor included, but the tutorial gets rather long so that the PIR sensor will be presented in the second part of this topic.

So let begins.

Step 1: LEDs Need Current to Be Limited

For the beginners, this is weird considering how usually to turn on an incandescent or fluorescent bulb. Not to worry, those LED bulbs on the shelf already come with an AC-to-DC power adapter and a limiting current. But would be nice to make something from the scratch.

One key to turn a LED is a current limiting circuit. This works as a valve for the current so that once the voltage applied to the LED making it becomes conductive, the current flows through the LEDs cannot be larger than the one regulated by the circuit. LEDs fails usually resulted from overheating. No current limiting circuit in place is a sure way to burn the LEDs up in a few seconds. By the way, fixing the LED chips, those come on the aluminum frame, to a heatsink reduce the LED chip and generally recommended.

I was scratching my head a lot when learning this around 2015, and still scratching my head (for different reasons). I learned from this tutorial and his clear explanation save me some cratches.

The circuit is attached. The bold lines signify the main load route, and the thinner ones indicate the control mechanism to control the current of the main route which is about 150 mA. Wikipedia has a short article and some references attached. The current limiting can be built with a transistor by replacing MOSFET with medium current load transistors such as BD135, BD139.

Step 2: Make a Flashlight

Parts needed:

  1. an N-channel MOSFET ( IRF540N $1.62/10pcs , 30N06, $.1.75/10pcs)
  2. an NPN transistor (such as S8085)
  3. 3.9 ohm – 2W resistor, the current limited around 0.6/3.9 = 153mA
  4. 100kR (1/4W) resistor
  5. A push-lock switch
  6. There 0.5W 8mm LED, $3.18/100pcs here
  7. A 18660 Lithium battery. I salvaged a few of them from a power bank. Laptop battery (4, 6, 8 cells) made from 18650 cell but careful with that stuff.
  8. 1 boost up to 12V board, like this one, $0.56
  9. 1 lithium charger such as this one, $0.30

The main difference to the circuit above is using a Lithium battery (18660) with boost up board instead of a mock-up 5V power adapter.

The last photo shows the final flashlight and it was charging from any micro USB source (>1A).

Step 3: Internet of Things With This Flashlight?

I break this part into three steps:

  • Prepare a subscriber (this one)
  • Setup MQTT server (next step)
  • and prepare a Publisher (the next one)


First, yes, I used that buzzword, aka IOTs, but this one could make a good combination of using the circuit in Step 2 with an ESP8266 and then control the light intensity by MQTT method.

Parts for the circuit are identical, except:

  1. Replace 12V boost up by MT3608 boost up board, $1.92/5pcs , this one can boost the voltage of a 18650 battery (about 3.7V) to 28 V, enough to power 8LEDs (instead of 3).
  2. The un-push position in the switch is connected to the GPIO 1 or 2 in the ESP8266 for PWM signal.
  3. ESP8266 01, $1.68 each. Like this one
  4. AMS1117 3.3 V, a voltage regulator for ESP8266, some capacitors
  5. A USB module to flash ESP8266, $0.78/pcs like this one

Btw. those links are for the convenience.

Soldering and uploading:

  • Soldering parts together, and use the schematics above. Some pre-made 3.3 voltage circuit may save you some time ($1.38/5pcs like this). I was confused to remember the pin when flipping the PCB board and ended up satisfy some AMS1117 voltage regulator.
  • Next. Make a small modification like in the third photo to hold down the GPIO 0 to GND, quickly tap the RST Pin with GND pin to put the ESP8266 into Flashmode.
  • Download the code here from my GitHub , and upload the code to ESP 8266 using Arduino IDE.

I highlighted the line to show the topic that the subscriber subscribed to. Any message posted to this topic will be passed along to other clients (subscribers) to this topic. The EPS8266 in this circuit will listen to the JSON message posted to the topic and sparge out if a change was made in channel Three.

* once upon a time, I thought I could put a pull-down resistor (100k) to ground the Gate of MOSFET when the ESP8266 was not presented. This works in the absence of an ESP8266, but with ESP8266, the resistor also pulls down the GPIO0 or 2 to GND, making them into a Flash mode or not boot up (when GPIO2 is LOW)). You will see a solid green if this happens.

Step 4: ​Set Up MQTT Server

MQTT (Message Queuing Telemetry Transport) is a messing protocol based on publish-subscribe method. Device hosting MQTT message in and out is called the broker. Like a real broker, it hosts the exchange between publishers (sellers) and subscribers (buyers). No money changes hands those. There are plenty of tutorials for this.

Here is the recap. Raspberry Pi is the most popular device for this. Frist, install MQTT by:

sudo apt-get install mosquitto mosquitto-clients

test if the MQTT server is working by open two terminals on Raspberry, one type the first line and the other for the next:

mosquitto_sub -h localhost -t "yourtopic"
mosquitto_pub -t "yourtopic" -h localost -m "say something"

You should see the "say something" shows up on the first terminal. Walah! It works.

The "#" could be use to listen to any topic, replace "yourtopic" by "#"

Now you don't want someone could publish or subscribe to your MQTT server, you should set up a password, doing so by:

cd ~
mosquitto_passwrd -c pwfile mqtt_user

mqtt_user can be other usernames that you like, enter the password twice, and update the .conf file by:

sudo nano /etc/mosquitto/mosquitto.conf

adds these two lines:

allow_anonymous false
password_file /home/pi/pwfile

then restart the mosquitto by:

sudo systemctl restart mosquitto

make a test by including the name with a password such as:

mosquitto_sub -h localhost -t "yourtopic" -u "mqtt_user" -P "123456"
mosquitto_pub -h localhost -t "yourtopic" -u "mqtt_user" -P "123456" -m "is this secured?"

Also if the MQTT refuses the connection, try this to put the MQTT service to the background of Linux system by:

mosquitto -d

I found these references are good to have a look. I learned something from these two today.

  1. Adafruit:

Step 5: ​Prepare a Publisher

For this one, I make a simple dashboard to push the message to MQTT server. In the stand-mode, the LCD shows the Clock.


  1. SSD1306 Screen display, $2.41 each
  2. EPS8266 WEMOS D1 Mini, $2.53 each
  3. A potentiometer
  4. One 4 pin slide switch.
  5. two 3mm LEDs,
  6. some resistors


Here is the schematics of this terminal:

Upload the code:

The branded WEMOS ESP8266 is pleasant to work with. You only need a micro USB, press the upload button on Arduino IDE flash the chip. The code is here (GitHub):

To publish a message, push the switch to turn the green led on (and red off), then slide up the pin and adjust, and finally push the button again to push the message onto MQTT server. The dashboard could publish a JSON message to 4 channels.

Take a look at the highlight on the code (screenshot). That is the topic the dashboard pushes the message to, and our LEDs is very eager to see any new from the JSON message.

That is about it. I hope the tutorial is helpful.