Introduction: Rotary Encoder Button
This is a rotary remote control based on a rotary encoder. It has the following features.
- Battery operated with very low current consumption when activated
- Automatic activation when control is rotated
- Automatic sleep after a period of inactivity
- Configurable actions when control is changed
- Simple web access with position reported
- MQTT reporting
- Control of Lighwaverf lamp dimmer
Step 1: Components and Tools Needed
The following components are needed
- Case - 3D printed enclosure available at
- ESP-12F ESP8266 module
- Rotary Encoder control (EC11) + Knob
- On/Off slide switch
- 3 pin socket
- LIPO battery 400mAh 802030
- 3.3V voltage refulator (xc6203)
- 220uF capacitor
- Resistors 1M, 4K7 (2)
- Schottky diode 1N5819 (2)
- P channel MOSFET (AO3401)
- Enamel wire (solderable)
- Hook up wire
The following tools are needed
- Fine point soldering iron
Step 2: Electronics
The electronics are based on an ESP-12F module. A small amount of support electronics is used to regulate the battery, and provide for auto switch on and off.
The power control is via the EN signal on the ESP-12F which has to be high for the module to power up. This is kept low by the 1M resistor but can be pulled high by the MOSFET transistor. The 4u7 capacitor gives a minimum several seconds of activation even after the MOSFET turns off.
The MOSFET transistor is initially turned on by one of the rotary encode switches being grounded as it turns. It can then be kept on by a GPIO signal as the ESP8266 code starts up.
The MOSFET turns off when the GPIO signal is released after inactivity for a timeout period is detected.
Step 3: Assembly
I did the following assembly steps.
- Print 3d enclosure
- Fit on/off switch and 3 pin charger point. Use resin to fix in place and clip down internal tags to minimum length
- Clip down legs on rotary control so they are flush with the base to minimize height
- Attach 4 wires to control. One side of the push button switch is connected to the central connector of the encode switches.
- Attach encoder in case and secure with its mounting nut. Add Knob
- Mount regulator on capacitor and attach wires from it to power pins on ESP-12F module
- Solder other electronic components on back side of ESP-12F module. I used enamel copper wire to hook these up. A short stub of wire was used on the EN pin as this has several components attached to it.
- Solder on hook up wire to the on / off switch
- Solder on battery leads to charger point and feed from on / off switch
- Solder on wire from on/off switch to regulator input.
- Attach the 4 wire from the encoder to the board.
Note I used smd components throughout to keep size as small as possible. If using larger components then you will probably need to increase height of the 3d enclosure. Likewise if you use a different size battery. The dimensions are easily adjustable in the SCAD file.
Step 4: Software
The software for this project is available at https://github.com/roberttidey/RotaryEncoderButton
It is an Arduino based project so set up an esp8266 Arduino development environment. You may want to set the passwords for WifiManager and software update in the ino file to something more sensible.
It should be compiled in Arduino ESP8266 IDE and serial uploaded to the module.
First time use will start an access point which should be connected to on a phone or tablet phone. See code for password. Browser on phone or tablet should then be used to access 192.168.4.1 which will allow selection of local wifi ssid and password. This only needs to be done once or if the wifi network changes. From then on the module will connect to local wifi network if required.
Some support files should also be uploaded. These are in the data folder of the git. They can be uploaded by accessing ip/upload. Once these have been uploaded then ip/edit may be used to do further upload in an easier fashion.
Step 5: Configuration
Configuration is contained in the rotaryEncoderConfig.txt file
This has two base parameters (host name and inactivity timeout milliseconds plus configuring up to 3 rotary encoders.
Although this build only uses 1 encoder, the library used allows for up to 3.
Each encoder has a line in the config file with a number of parameters.
- pin1, pin2, button GPIO pins
- min value of encoder
- max value of encoder
- starting position value (last value is also remembered when activated.
- type of action to perform 0=None,1=web GET,2=UDP / Lightwave,4=MQTT
- interval is minimum duration in ms between actions
- action template is basic template for action
- par1, par2, par3, par4, par5 are additional parameters
The action template has variables which get substituted before use
- $p Rotary position
- $d Rotary direction
- $e encoder number (0 is first)
- $l makes a lightwave function
- $x,$y,$z,$u,$v substiute par1 - par5
- $t substitute change counter
- $c substitute comma
Step 6: Operation
After wifi is set up then the control is activated by clicking it one step either way. This does not change the position or trigger an action.
From then on any rotation will trigger the configured action. The minimum action interval will potentially limit the actions performed as the control is rotated. For example if the minimum interval is 2000mS then a rapid rotation may send just the first and last change. The last position will always trigger an action so if a dimmer is being controlled then its value will reflect the end position even if some intermediate steps are missed out.
An example is shown in the supplied config file. The basic action is UDP to the Lightwaverf link. The host ip and port number are put in par1 and par2. The Room/Device string are put in par3.
The link must first be paired to the link to allow it to accept commands from the mac address of the esp8266. To do this place a file called initLink,txt into the filing system and then send a command by rotating the control one step (after activating). This will send a pairing command to the link which must be acknowledged by pressing a button on the link. The initLink file is automatically deleted.
The device can be put into a maintenance mode where it will not turn off automatically, by turning on with the rotary control pushed in. To get it out of this mode just turn off and back on again.