Skip to content

Search the documentation

Kit
Firmware

Fan dial — Smart Home

Turn the little knob (the potentiometer) and the fan speeds up and slows down to match. Turn it all the way back and the fan stops.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Start
  • Print to serial "Turn the dial to change the fan"
  • repeat forever
    • Print to serial (create text with "Knob: " Read Potentiometer)
    • Set Fan Speed (Read Potentiometer ÷ 41)
    • wait 0.5 seconds
  • A Smart Home board with the potentiometer and the fan.
  • A USB cable and the serial monitor.
  • Keep hair, fingers and paper away from the fan blades.
  1. Open 07-fan-dial.mobot in MoBot.
  2. Plug in, pick the port, press Send to Device.
  3. Turn the knob slowly from one end to the other, and back.
  • Twice a second the serial monitor prints the knob’s position:

    [MOBOT] Turn the dial to change the fan
    [MOBOT] Knob: 0
    [MOBOT] Knob: 812
    [MOBOT] Knob: 2044
    [MOBOT] Knob: 4095

    0 at one end, about 4095 at the other. If the number never moves, the knob is not the knob you are turning. If it jumps to 4095 and stays there, it is turned all the way already.

  • The fan starts spinning somewhere around the middle of the dial, and gets faster and louder from there. Hold your hand a few centimetres away and feel the air. Turn the knob back down and it stops.

The fan does not start at the very bottom of the dial. A small motor needs a real push to get going, so the first third of the dial changes the number without moving the blades. That is not a fault — and it is why the program prints the number: the numbers tell you the program works long before the fan agrees.

If the number wobbles by one or two while you hold the knob still, that is normal. The board is measuring a real voltage, and the last digit dances.

The knob gives back a raw reading from 0 to 4095. The fan wants a percentage, 0 to 100. So the program divides:

knob ÷ 41

4095 ÷ 41 is 99.9 — near enough to “all the way”. The answer usually comes out with a long tail of decimals (49.87804878…) and that is fine: the fan is happy with a decimal, and anything above 100 or below 0 is trimmed by the board itself.

There is no rounding block in MoBot, so the decimals stay. See the notes at the bottom.

block what it does
iot_pot Reads the knob. 0 to 4095.
math_arithmetic The ÷ 41.
iot_fan Sets the fan speed, 0 to 100. The speed is a socket, so a sensor can drive it.
iot_print, text_join The running number.
wait Twice a second is plenty; the knob is not going anywhere.

The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.

07-fan-dial.firmware-3.main.py
# Fan dial — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 07-fan-dial.mobot and press Send to Device. It is GENERATED from the blocks;
# editing this file changes nothing. Edit the blocks instead.
# The firmware 2.x version is 07-fan-dial.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Turn the dial to change the fan')
while True:
print("[MOBOT]", ('Knob: ' + str(home.knob.position())))
home.fan.set_speed((home.knob.position() / 41))
time.sleep(0.5)
  • Divide by 82 instead of 41. Now the whole dial only reaches 50% — a fan with a “quiet mode”.
  • Swap iot_pot for iot_ldr and you have a fan that runs faster in bright light. Shine a torch at the sensor and blow your own hair back.
  • Swap it for iot_dht set to Temperature and divide by 1 instead of 41 — now the fan runs harder as the room warms up, which is what a real fan is for.