Skip to main content

Temperature Controlled Desk Fan, with XinaBox and Zerynth

This tutorial explains how to use the Bosch BME280 weather sensor (xChip SW01), and specifically, temperature data, to control a desk fan, via a solid state relay (xChip OC03). We programmed in Python using Zerynth Studio - a partner of XinaBox & RS

Parts list

Qty Product Part number
1 USB Programming Interface Module for FT232R, IP01 174-3703
1 WI-FI & BLUETOOTH CORE (ESP32),CW02 174-3702
1 XinaBox OC03 Relay Out for PCA9554A Module 174-3715
1 Advanced Weather Sensor (BME280) 174-3744
1 AVX BTB Series, Male PCB Edge Connector SMT, 10 Way, 2 Row, 2mm Pitch, 2.5A 174-4977
1 Zerynth Studio
1 Bell wire


XinaBox’s modular and easy-to-code hardware allows users to assemble an electronic circuit in minutes, without soldering, wiring or hardware knowledge.

This tutorial explains how to use the Bosch BME280 weather sensor (xChip SW01), and specifically, temperature data, to control a desk fan, via a solid state relay (xChip OC03). In a future post, we will cover how to make this control more sophisticated using the Zerynth app to allow wireless live control.

Hardware Setup

Connect the xChips according to the photograph in Figure 1, making sure that the xChip name and XinaBox logo are all visible from the same side.

Ensure that the switches on the IP01 xChip are in position “B” and “DCE”.

IMG_1204-2_d0c8916ee03ad415826231e7f451bd0aaa52284a.jpg IMG_1210-2_8201c8cef6947fca689c77d4c10aff159b68e79a.jpg

Figure 1: xChips assembled                Figure 2: xChips and fan

Connect bell wire to the OC03’s terminals. As shown in Figure 2, cut either of the Fan’s USB power wires and connect the two ends to the bell wire, creating a switch.

Zerynth Setup

If you are already familiar with Zerynth, virtualize your device and jump to Connecting the Device section.

For those of you who are using Zerynth for the first time, follow these steps:

  • Once your device is properly virtualized in Zerynth Studio as a XinaBox CW02 (ESP32) board, you can continue to the next step

Programming the Device

Create a project and copy the following code into Zerynth Studio, in the tab of your project and uplink it to the board:

# Temperature Controlled Desk Fan

# Created at 2018-10-03 by Daniel Berman, XinaBox

# Turn on fan via OC03 when SW01 temp > a set temperature (here 25 degrees Celsius)

# import xChips and settings

import streams

streams.serial()                # opens serial monitor

from bosch.bme280 import bme280

SW01 = bme280.BME280(I2C0, 0x76, 100000)

from xinabox.oc03 import oc03

OC03 = oc03.OC03(I2C0)



pinMode(D26,OUTPUT)             # defining CW01 LED output

temp_threshold = 25             # setting threshold temperature for fan operation

# loop forever

while True:

  tempC = SW01.get_temp()

  if tempC > temp_threshold:

      OC03.writePin(True)      # turn on OC03 relay

      digitalWrite(D26, HIGH)  # turn the CW01 LED ON by setting the voltage HIGH

      print("Temperature sensor shows " + str(tempC) + ", which is above the threshold of " + str(temp_threshold))

      sleep(2000)              # wait for 2 secs


      OC03.writePin(False)     # turn off OC03 relay

      digitalWrite(D26, LOW)   # turn the CW01 LED OFF by setting the voltage LOW

      print("Temperature sensor shows " + str(tempC) + ", which is below the threshold of " + str(temp_threshold))

      sleep(2000)              # wait for 2 secs



Figure 3: screenshot of Zerynth Studio

On running, the serial monitor shows the results of one of the print statements (see Figure 4), depending on whether the temperature is above or below the set threshold. This is useful to ensure our IF statement is working properly, and also to see the temperature data, so we know the sensor information is being properly received by the CW01 and being received correctly by Zerynth.

If the temperature is above the threshold, the CW01 LED will light, and the OC03 completes the circuit, switching on the fan.



Figure 4: Serial monitor output


Please see below video showing the fan operation; initially, the fan switches off again quickly, as the fan's airflow lowers the temperature sensor reading. When the fan is repositioned not to blow on the sensor, the temperature sensor stays above 25 degrees Celsius, and the fan (and LED) stays on.

Need more info? Espressif Systems has published an article on their blog about Zerynth tutorials for XinaBox devices. A neat and useful overview that everyone needs to check out, no matter their skill level.


I love technology and am Co-Founder of modular electronics company, XinaBox.