Build a Temperature Controlled Desk Fan in Python

Use a temperature sensor and solid state relay to control a desk fan, using XinaBox xChips and Zerynth Studio Python ecosystem.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Things Used in This Project

Hardware components

Software apps and online services

Step 2: Story

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.


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.

Step 3: 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”.

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.

Step 4: 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:

Step 5: 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 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 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.

Video of fan and LED operation

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.

Don't forget to check out the XK12 IoT Starter Kit, that enables you to build this and lots of other devices.

Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest



    7 months ago

    This is a great idea! I could see this being used to cool some of my old game systems. :)