I2C Collision-Avoidance Robot
Run the I2C Collision-Avoidance Robot MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim
This robot has one sensor: a VL53L0X distance sensor on the Inter-Integrated Circuit (I2C) bus. The picture is a view from above. The robot drives around a rectangular arena. The Pico asks the sensor "how far away is the wall?" again and again.
Here is the plan the robot follows:
- Drive forward.
- If the sensor says the wall is 15 cm away or closer, stop going forward.
- Back up 20 cm.
- Flip a coin. Heads, turn right. Tails, turn left. (Each has a 50% chance.)
- Drive forward again.
The MicroSim also shows what travels on the I2C bus. Each read is a short transaction:
START, the chip address, the register number, a restart, the address again, two data
bytes, and STOP. The two data bytes are the distance in millimeters. The bytes
01 2C mean 300 mm, which is 30.0 cm.
Learning objective: You will be able to predict how the read rate and the robot's speed change how close the robot gets to a wall before it reacts.
- Bloom's Taxonomy (2001): Apply - use, predict
- Interaction pattern: two sliders (reads per second and robot speed) with live feedback
How to Use
- Press Start. The robot drives, and the Pico starts reading the sensor.
- Watch the red cone. It shows what the sensor can see. The number on the wall is the latest reading.
- Watch the I2C bus panel. Each read lights up the frame box by box, and the two data bytes appear last.
- Look at the black log. Find the line marked
TOO CLOSE!in red. That is the read that makes the robot back up. - Watch the backing up progress bar fill to 20 cm. Then watch the coin flip and the turn.
- Let it run for a minute. Compare the Left turns and Right turns counts. They should end up close to equal.
- Now try an experiment. Set Reads per second to 1 and Robot speed to 40 cm per second. Predict what will happen, then press Start.
What to Look For
- A read, then a decision. The robot only knows the distance when a read finishes. Between reads it is driving blind.
- Reaction distance. Distance traveled between reads = speed divided by reads per second. At 20 cm per second and 10 reads per second, that is 2 cm. At 40 cm per second and 1 read per second, it is 40 cm, which is more than the whole safety gap. Then you will see BUMP!
- The robot may stop a little inside 15 cm. The 15 cm rule triggers on the first read at 15 cm or less. The robot keeps moving until the next read.
- The coin flip is fair, but not exact. After 10 turns you may see 7 left and 3 right. After hundreds, the counts get close to equal.
- Only one device is on the bus. The address is
0x29, which is 41 in decimal.
Embedding This MicroSim
You can add this MicroSim to any web page with this HTML:
1 2 3 4 | |
Lesson Plan
Grade Level
Ages 10-18. Part of Appendix A: The I2C Bus.
Duration
10-15 minutes
Prerequisites
- The I2C Bus Scanner MicroSim
- Chapter 9: Temperature and Distance Sensors
- Chapter 13: Robots and Mobile Systems
Activities
- Predict (3 min): Before pressing Start, students guess how many times the robot will bump into a wall in one minute at the default settings. (Answer: none.)
- Follow one read (3 min): Students find the read marked
TOO CLOSE!and say which two bytes made the robot back up. - Break it (4 min): Students set 1 read per second and 40 cm per second. They count the bumps and explain why they happen.
- Fix it (3 min): Students find two settings changes that stop the bumps.
- Check the coin (2 min): After a minute, students compare the left and right turn counts.
Assessment
- The student explains that the robot only knows the distance when a read finishes.
- The student predicts that a slower read rate or a faster robot gives more bumps.
- The student reads two data bytes (such as
00 96) and says the distance in millimeters and centimeters.
The Code Behind It
This is the same plan as the book's Maker Pi RP2040 collision-avoidance robot.
The motor functions (forward, reverse, turn_left, turn_right) are on that page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
| Line | What it does |
|---|---|
tof = VL53L0X(i2c) |
Sets up the distance sensor on the I2C bus |
distance_mm = tof.read() |
Reads the two data bytes and turns them into millimeters |
distance_cm = distance_mm / 10 |
Changes millimeters into centimeters |
if distance_cm <= TURN_DISTANCE_CM: |
Checks the 15 cm rule |
reverse() and sleep(REVERSE_TIME) |
Backs up. The time sets how far (about 20 cm) |
if urandom.random() < 0.5: |
The coin flip. random() gives a number from 0 up to 1 |
turn_right() or turn_left() |
Turns the robot, then sleep(TURN_TIME) waits for the turn |
References
- Maker Pi RP2040 Collision Avoidance Robot - the book's robot that uses the same plan
- VL53L0X Time-of-Flight Distance Sensor - the sensor and its driver
- Pololu VL53L0X library - the range result is a 16-bit number at register
0x1E - UM10204: I2C-bus specification and user manual, Rev. 7.0 - NXP, 2021
- Appendix A: The I2C Bus - the full explanation that goes with this MicroSim
- p5.js - the JavaScript library used to build this MicroSim