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 program
Section titled “The program”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
- Print to serial (create text with
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”- Open 07-fan-dial.mobot in MoBot.
- Plug in, pick the port, press Send to Device.
- Turn the knob slowly from one end to the other, and back.
What you should see, hear and feel
Section titled “What you should see, hear and feel”-
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: 40950 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.
Where the fan’s speed comes from
Section titled “Where the fan’s speed comes from”The knob gives back a raw reading from 0 to 4095. The fan wants a percentage, 0 to 100. So the program divides:
knob ÷ 414095 ÷ 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.
The blocks
Section titled “The blocks”| 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 Python code
Section titled “The Python code”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.
# 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 Pinimport 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)# Fan dial — iot## This is what MoBot puts on the board 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.
from machine import Pinimport time
print("[MOBOT]", 'Turn the dial to change the fan')while True: print("[MOBOT]", ('Knob: ' + str(mobotIOT.pot.read()))) mobotIOT.fan.set_speed((mobotIOT.pot.read() / 41)) time.sleep(0.5)Try changing it
Section titled “Try changing it”- Divide by 82 instead of 41. Now the whole dial only reaches 50% — a fan with a “quiet mode”.
- Swap
iot_potforiot_ldrand 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_dhtset 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.