First Sensor App
By the end of this tutorial your device will show a live sensor reading on screen โ updating in real time as you interact with the sensor.
What youโre building
Section titled โWhat youโre buildingโA UIFlow program that:
- Reads a value from an external sensor (temperature/humidity, or motion)
- Stores it in a variable
- Displays it on screen with a label
The program runs in a forever loop so the reading updates continuously.
Step 1 โ Add the sensor as a Unit
Section titled โStep 1 โ Add the sensor as a UnitโIn UIFlow, external sensors are added as Units before you can use them in blocks.
How to add a Unit:
- Click the Units button in the left sidebar (looks like a plug icon)
- Click + Add Unit
- Find your sensor in the list and select it
- Click OK โ UIFlow 2.0 detects the connection automatically
- The unit now appears in your block palette
ENV III measures temperature and humidity.
- In the Units list, select ENV III
- After adding, youโll see ENV III blocks in the palette:
get temperature,get humidity,get pressure
Which value to display: Start with get temperature โ itโs the most satisfying to demonstrate (breathe on the sensor and watch the number change).
M5Stackโs docs page for ENV III includes a short tutorial video: docs.m5stack.com โ Units โ ENV III
PIR detects motion โ it outputs True when motion is detected, False when still.
- In the Units list, select PIR
- After adding, youโll see a
PIR get stateblock
Note: PIR returns a boolean (True/False), not a number. Youโll display Motion detected! or No motion instead of a number.
M5Stackโs docs page for PIR includes a short tutorial video: docs.m5stack.com โ Units โ PIR
Step 2 โ Read the sensor value in blocks
Section titled โStep 2 โ Read the sensor value in blocksโCreate the reading logic inside the loop section:
- Open the Blocks editor (not MicroPython)
- Find the loop section on the canvas โ UIFlow 2 creates this automatically. Anything inside it repeats forever. If you see separate
SetupandLoopblocks, use theLoopblock. - Inside the loop, drag in Variables โ Set [variable] to [value]
- Create a new variable called
sensorValue - Set
sensorValueto the sensor read block:- ENV III: drag
ENV III get temperatureinto the value slot - PIR: drag
PIR get stateinto the value slot
- ENV III: drag
- Add a Time โ Wait block at the bottom of the loop: set it to 500 ms (0.5 seconds)
This loop will read the sensor twice per second and store the result.
Step 3 โ Display the value on screen
Section titled โStep 3 โ Display the value on screenโAdd a Label widget to show the sensor reading:
- Switch to the UI tab (screen design view)
- Drag a Label widget onto the screen
- Give it a sensible default text like
--- - Note the labelโs variable name (e.g.,
label0) โ youโll use this in blocks
Back in the Blocks editor, inside your forever loop (after the sensor read):
- Drag a Label โ set text block
- Select
label0(or whatever your label is named) - Set the text to
sensorValue
For ENV III, you may want to add a unit string. Use a Text โ join block to combine the number with " ยฐC":
set label0 text to: join(sensorValue, " ยฐC")Step 4 โ Style the screen
Section titled โStep 4 โ Style the screenโMake it look intentional:
- In the UI editor, click the screen background and pick a colour (dark blue works well)
- Click your label โ find the font size control in the properties panel and increase it until the text is large and easy to read on the M5Stack screen
- Add a second label above it with the sensor name as static text (e.g.,
Temperature) - Set the static label to a smaller size and lighter colour
Step 5 โ Run and test
Section titled โStep 5 โ Run and testโClick the Run button (โถ) in UIFlow.
ENV III: Breathe gently on the sensor โ the temperature should tick up within a few seconds.
PIR: Wave your hand in front of the sensor โ the display should change between True and False (or Motion detected! / No motion if you added a conditional).
Troubleshooting
Section titled โTroubleshootingโ| Problem | Try this |
|---|---|
| Unit doesnโt appear in palette | Remove and re-add it via Units โ make sure the sensor is physically connected before adding |
Screen shows --- and never updates | Check the forever loop is running โ make sure you clicked Run |
| Temperature reads 0.0 | The cable may be loose โ unplug and reconnect, then re-run |
| PIR always shows True | Cover the sensor with your hand briefly to reset it, then uncover |
| UIFlow disconnects mid-session | Reconnect via the device menu โ your blocks are saved in the browser |
| Device freezes or crashes on startup | Both sensors are plugged in at once โ unplug one, press the reset button on the side of the device, then reconnect |
Was this page helpful?