• 2-button switch project with integrated temperature and humidity sensor

2-button switch project with integrated temperature and humidity sensor

Some time ago, we completed a project for a building restorer who asked us to build a wall (surface) mounting light switch and also to provide the necessary sensors to monitor temperatures and humidities in a singular building here in Spain. We quickly understood that minimizing the visual impact was a major requirement so we decided to create light switches with temperature and humidity sensor included. Reducing the amount of boxes by half was in fact very welcome by our customer.

Since we wanted something similar for our workshop we recently took again over this concept and tried to simplify it down to the minimal expression. We sourced some arcade buttons from eBay and put everything into our current-meter enclosure, which measures only 25 mm in depth. The result looks quite funny. Isn’t it?


This time we used our minibat board with an external 2xAAA battery holder and on-board SI7021 temperature & humidity sensor. The PCB antenna is more than enough for our needs and two AAA batteries should provide around a year of autonomy as soon as a reasonable use is given to this device.



We configured the board to transmit values also periodically, every six minutes. Wiring is quite simple as you can see in the above picture.

  • D15 -> connected to one of the terminals of first button
  • D14 -> connected to one of the terminals of second button
  • GND -> connected to the second terminal of both buttons

As always, the battery holder has to be wired to VCC (red wire) and GND (black wire), according to minibat’s pinout diagram.

We had to cut out one of the buttons since it needed to fit just onto the battery holder. Otherwise, adding the holes on the cover, placing the buttons and wiring the circuits was very straightforward.



We also added some drills on the top and on the bottom of the enclosure in order to allow air to flow through the device. This is very important in fact when you want to measure ambient temperature and humidity.

Why does panStamp fit this kind of applications so well when compared to other technologies? Well, we need to highlight that this wall switch NEEDS to be powered from a couple of AAA batteries and that transmissions have to be completed immediately after pressing one of the buttons. These low-power and real-time requirements make us discard WiFi by default. We also need to connect these devices to the Internet by means of a gateway so Bluetooth and its typical device-to-phone paradigm is not really appreciated in this kind of projects. Low-power ISM radios seem then to fit our needs but which one? Having medium to long ranges is always good, even indoors, so lets concentrate on sub-1GHz technologies and discard the rest (2.4 GHz). OK, now let’s say that we want something programmable from the Arduino IDE, with available code and carrier boards to measure temperature and humidity, with available IO pins and with a decent gateway to connect to any other software via MQTT… OK, we have reduced the list down to maybe two or three options, being panStamp one of them. Is there anything else we should say?

The Arduino sketch, called htupanel, is just an evolution of our classic htusensor sketch that relies on Sparkfun’s HTU21D library (only version 1.1.1 works with panStamp NRG for some reason). This library is also compatible with Silicon Labs’ SI7021 sensor, the one assembled onto the minibat board. Running lagarto-swap or any other SWAP-enabled application should automatically recognize this device and expose its IO resources to the user.

Looking into htupanel we can distinguish three main pieces of code, featuring exceptional simplicity and functional contents. Firstly, we need to enable binary inputs as interrupts:

pinChange is simply an empty function. It does nothing but a button press (pin to GND) will wake up the processor from sleep mode and loop will resume right after swap.goToSleep().

Now let’s take a look into the loop:

Nothing more than a call to the register updater (with SWAP status transmission included) and our popular goToSleep method, which brings the current consumption down to under 2 microamps. goToSleep releases the processor from sleep periodically and also upon button press. Finally, getData is internally (SWAP stack) pointing to a custom application impmeneted in regtable.ino, called updtSensor:

Where dtSensor is a byte array used as data container for the SWAP register regSensor. This register includes four different pieces of information:

  • Bytes 0 and 1 : battery voltage in millivolts
  • Bytes 2 and 3 : (temperature + 50) * 10, in degrees Celsius
  • Bytes 4 and 5 : humidity, in 0.1% steps
  • Byte 6 : Eight binary (bit) states. We are simply taking the first two inputs for our wall switch

SWAP packets received by lagarto-swap are then immediately transferred to node-red via MQTT. We can create logic relationships from node-red between ambiance data (temperature and humidity), button presses and a bunch of output-boards that we have here in the workshop to control lights, heaters and doors. Our smart workshop project was a matter of a couple of hours in fact, once the hardware was installed and wired.

Our next domestic project will probably consist in some emergency buttons here and there to switch mains electricity off in case of accident and also to send some telegrams from node-red.

For further questions about panStamp (hardware, programming, etc) and SWAP please refer to our wiki and forums. You can of course also contact us via e-mail and phone with your inquires. We will be more than glad to assist you.


Comments are closed.