Hands-on: four physics gauges on screen
Module 3 — Processing with maths and physics · Slides: slides.md · Module overview · Course page
Fill the four conversion formulas in s06_physics_viz.py (tilt, energy, altitude, dBFS), then pick a quantity from the dropdown, move, lift or make noise, until the Seg7, bar and chart move with the values we computed ourselves.
Objectives
Section titled “Objectives”By the end of this lesson, you will:
- Fill the four formulas in practice/s06_physics_viz.py until at least three quantities (Tilt, Energy, Altitude) show values that follow real motion on the emulator or the board.
- Test the energy formula so it reads about 0 at rest and above 1 when shaken hard, and explain why it must divide by 9.81 and subtract 1 g.
- Explain the role of the placeholders in the practice file, capturing p0 before the loop, and releasing the microphone with pdm.deinit() in finally.
Before you start
Section titled “Before you start”You’ve been through lesson 3.1, and understand the four formulas and the reasoning behind dividing by 9.81. Fill in one formula at a time and test one quantity at a time — this catches bugs more easily than filling everything in at once.
- Hardware: a TESAIoT Dev Kit board already flashed with BENTO’s MicroPython firmware, or the BENTO Emulator inside BENTO IDE — Tilt, Energy and Altitude work fully on the emulator; Sound on the emulator is synthetic. Real sound needs a board whose PDM microphone works (see the TESAIoT Dev Kit’s limitation in lesson 2.3).
- Prior lesson: lesson 3.1 — From raw numbers to physical quantities: tilt angle, energy, altitude and dBFS
Concepts
Section titled “Concepts”The whole file reads as one sentence: choose a quantity → read the raw value → compute it into a real quantity → send it to Seg7, Bar and Chart → loop every 120 ms. The top of the file creates widgets once, tries opening the mic inside a try to set has_mic, and captures p0 before the loop — all of this is already given. Our job is the four formulas in the middle (the derived beat). The practice file leaves placeholders such as roll, pitch = 0.0, 0.0 or db = -96.0 so it runs (showing 0) even before you fill anything in — the values only move once you fill in the real formula, the same technique as r = None in lesson 1.3.
The four formulas are: (1) roll, pitch = dsp.tilt(ax, ay, az) (2) mag = math.sqrt(ax*ax + ay*ay + az*az) / 9.81, then energy = abs(mag - 1.0) (3) alt = dsp.altitude(p, p0) and (4) db = 20 * math.log10(rms / 32768.0) inside if rms > 0:, because log10(0) can’t be computed. Watch the order dsp.tilt returns (roll before pitch), and remember motion() gives units of m/s² — if you forget to divide by 9.81, energy stays stuck around 8.8 even at rest. Pressure is sensitive to wind and air conditioning, so the altitude value naturally jitters a little — we’ll filter it steady in module 4.
Once everything’s filled in, the Chart keeps a history for you. Try shaking rhythmically and watch the waveform — you’ll see “the shape of the motion” much more clearly than a plain still number. Success isn’t just the numbers moving — you should be able to say what energy 0.8 actually means, and why tilt can use atan2 without ever needing to know accel’s unit.
Worked example
Section titled “Worked example”s06_physics_viz_full.py adds remembering the highest and lowest values (hi/lo hold), a STEADY/ACTIVE label, and a Reset button. Run it before starting the lesson to see where you’re headed, then come back and read it again once your practice file passes.
| File | What this file teaches |
|---|---|
| examples/s06_physics_viz_full.py | Physics Lab: raw -> derived -> viz (full version) |
Practice
Section titled “Practice”The 4 # TODO: comments are at lines 94, 103, 112 and 125 (Tilt, Energy, Altitude, dBFS). Replace the placeholders with the formulas the hints describe, then press Run (emulator) or Program to Device (board). If a gauge stays stuck at 0, check that you filled in the formula line and didn’t leave a placeholder overwriting it afterward.
| Practice file | Topic |
|---|---|
| practice/s06_physics_viz.py | Physics Lab: raw -> derived -> viz (the fill-in-the-code version) |
Solution
Section titled “Solution”Open the solution after trying on your own at least once, and read how to use the solutions first.
| Solution | Pairs with |
|---|---|
| solution/s06_physics_viz.py | practice/s06_physics_viz.py |
Check your understanding
Section titled “Check your understanding”The same questions are in quiz.yaml for automated checking.
-
You filled in the Tilt formula, but the angle gauge stays stuck at 0 every time. What’s the likely cause? (single choice · objective 1)
- a) You haven’t replaced pass yet, or a placeholder line roll, pitch = 0.0, 0.0 still remains after the formula
- b) dsp.tilt only works on a board
- c) You must divide by 9.81 before calling dsp.tilt
- d) Chart can’t accept values above 100
Solution
a — the placeholder exists to let the file run before you fill anything in. If it’s still overwriting the value after your formula, the gauge stays at 0 forever. dsp.tilt, on the other hand, needs no unit conversion first.
-
With the board lying still, Energy reads 8.81 constantly. How should the formula be fixed? (single choice · objective 2)
- a) Change abs to max
- b) Divide the vector’s magnitude by 9.81 to get units of g, before subtracting 1.0
- c) Remove the subtraction of 1.0 from the formula
- d) Use gx, gy, gz instead of ax, ay, az
Solution
b — motion() gives m/s², so at rest the magnitude is about 9.81. Dividing by 9.81 gives about 1 g, and subtracting 1 leaves about 0.
-
energy (in g) is converted into a bar with clamp100(energy / 2.0 * 100). If energy = 0.5, where does the bar sit? (single choice · objective 2)
- a) 5
- b) 25
- c) 50
- d) 100
Solution
b — 0.5 / 2.0 × 100 = 25. The range 0..2g is mapped onto a 0..100 bar; anything above 2g gets clamped to 100.
-
Which of these are true about the parts already given in the practice file? (select every correct answer) (multiple choice · objective 3)
- a) p0 is captured once before the loop, because it’s a reference point that doesn’t change
- b) pdm.deinit() sits in finally, so the microphone is always released no matter how you exit
- c) The placeholders let the file run and show 0 before anything’s filled in
- d) Widgets are recreated inside the loop every 120 ms
Solution
a, b, c — widgets are created once before the loop, per the shared skeleton, with only their values changing inside it. The other three are correct set-up before the loop and correct cleanup.
The MVP for lessons 3.1–3.2: a raw signal → a computed quantity → shown on screen, the full loop working for at least one quantity.
- All four formulas in the practice file are filled in, and it runs on the emulator or the board.
- Test at least three quantities (Tilt → Energy → Altitude), and note in your learning log what you had to do to see each value clearly change.
- Find Energy’s reference value at rest, then temporarily remove
- 1.0and see what the value at rest becomes, and explain why. - Be able to explain what quantity and unit each formula you filled in converts the raw number into.
Going further
Section titled “Going further”In the next pair of lessons (3.3–3.4), we’ll take the quantities we’ve computed and judge them with rules — such as “comfortable” or “hot” from temperature and humidity.
Next lesson: lesson 3.3 — Derived values and rule-based classification: dew point, heat index and a rule ladder
Reflect
Section titled “Reflect”- Which quantity does the Chart tell a better story with than a plain Seg7 number, and why?
- If you had to use the energy value to decide “someone picked up the board,” what threshold would you set, and how would you test that threshold?
Review questions
Answer on your own first, then open the answer.
-
You filled the Tilt formula but the angle gauge stays at 0. What is the likely cause? (Objective 1)
- ยังไม่ได้แทน pass หรือยังเหลือบรรทัด placeholder roll, pitch = 0.0, 0.0 อยู่หลังสูตร
- dsp.tilt ใช้ได้เฉพาะบนบอร์ด
- ต้องหาร 9.81 ก่อนเรียก dsp.tilt
- Chart รับค่าเกิน 100 ไม่ได้
Show answer
Answer: A. ยังไม่ได้แทน pass หรือยังเหลือบรรทัด placeholder roll, pitch = 0.0, 0.0 อยู่หลังสูตร
placeholder มีไว้ให้ไฟล์รันได้ตอนยังไม่เติม ถ้ามันยังทับค่าหลังสูตรอยู่ เกจจะโชว์ 0 ตลอด ส่วน dsp.tilt ไม่ต้องแปลงหน่วยก่อน
-
At rest the Energy view shows 8.81 all the time. How should the formula change? (Objective 2)
- เปลี่ยน abs เป็น max
- หารขนาดเวกเตอร์ด้วย 9.81 ให้เป็นหน่วย g ก่อนลบ 1.0
- ลบ 1.0 ออกจากสูตร
- ใช้ gx gy gz แทน ax ay az
Show answer
Answer: B. หารขนาดเวกเตอร์ด้วย 9.81 ให้เป็นหน่วย g ก่อนลบ 1.0
motion() ให้ m/s² ขนาดตอนนิ่งจึงราว 9.81 หาร 9.81 แล้วได้ราว 1g ลบ 1 จึงเหลือราว 0
-
Energy in g is mapped to the bar with clamp100(energy / 2.0 * 100). If energy = 0.5, where is the bar? (Objective 2)
- 5
- 25
- 50
- 100
Show answer
Answer: B. 25
0.5 / 2.0 × 100 = 25 ช่วง 0..2g ถูก map ลงแถบ 0..100 ส่วนค่าเกิน 2g จะถูก clamp ไว้ที่ 100
-
Which statements about the parts already given in the practice file are correct? (select all that apply) (Objective 3)
- p0 จับครั้งเดียวก่อนลูปเพราะเป็นจุดอ้างอิงที่ไม่เปลี่ยน
- pdm.deinit() อยู่ใน finally เพื่อคืนไมโครโฟนเสมอไม่ว่าจะออกด้วยวิธีใด
- placeholder ทำให้ไฟล์รันได้และโชว์ 0 ตั้งแต่ยังไม่เติม
- widget ถูกสร้างใหม่ในลูปทุก 120 ms
Show answer
Answer: A. p0 จับครั้งเดียวก่อนลูปเพราะเป็นจุดอ้างอิงที่ไม่เปลี่ยน · B. pdm.deinit() อยู่ใน finally เพื่อคืนไมโครโฟนเสมอไม่ว่าจะออกด้วยวิธีใด · C. placeholder ทำให้ไฟล์รันได้และโชว์ 0 ตั้งแต่ยังไม่เติม
widget สร้างครั้งเดียวก่อนลูปตามโครงร่วม ในลูปแค่เปลี่ยนค่า ส่วนอีกสามข้อคือการเตรียมของก่อนลูปและการเก็บกวาดที่ถูกต้อง
Cite this lesson
If you teach from this lesson or reuse it in slides or documents, credit it with the text below. If you changed it, add (adapted) after the title.
"Hands-on: four physics gauges on screen" from TESA Open Knowledge by the Thai Embedded Systems Association (TESA), https://github.com/tesaiot/tesa-qualification-program, licensed under CC BY-NC 4.0
Thai attribution: "ลงมือทำ: เกจฟิสิกส์สี่ตัวบนจอ" จาก TESA Open Knowledge โดยสมาคมสมองกลฝังตัวไทย (Thai Embedded Systems Association: TESA) https://github.com/tesaiot/tesa-qualification-program สัญญาอนุญาต CC BY-NC 4.0
TESA Open Knowledge · © 2026 สมาคมสมองกลฝังตัวไทย (TESA) · CC BY-NC 4.0
Content is licensed CC BY-NC 4.0. Reuse it non-commercially and credit the Thai Embedded Systems Association (TESA) every time. · How to cite TESA