Lesson plan

Accelerometers, gyros and IMUs lesson plan

What a motion sensor really measures: gravity as a vector, tilt from atan2, gyro drift and calibration, the complementary filter, a magnetometer compass, step and tap detection, and a tilt-compensated level and compass.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • An accelerometer measures gravity as a vector: three numbers, raw counts, and the angle they hide.
  • A gyro added up into an angle, the drift that comes with it, and the one-line filter that fixes it.
  • A magnetometer compass and its traps, then a stream of readings turned into steps and taps.
  • A builder’s level and a compass that stays true when you tip it, on an MPU-9250.

Materials for the whole unit

  • Accelerometer and gyroscope, MPU6050
  • Accelerometer, ADXL345
  • Arduino Uno R3
  • Half breadboard
  • LED x 2
  • Magnetometer, LIS3MDL
  • Nine-axis IMU, MPU-9250
  • Resistor x 2
  • Six-axis IMU, LSM6DSOX

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 40 min

Which way is down

An accelerometer measures gravity as a vector: three numbers, raw counts, and the angle they hide.

LessonTimeWhat students do
Gravity as a vectorRead an MPU6050 straight from its registers and watch 1 g of gravity split across three axes as you tip it. Predict each number with sines and cosines, read the raw counts, and find the one thing an accelerometer cannot tell apart.50 min
  1. Run it
  2. Tip it 30 degrees
  3. Counts, not g
  4. Two tilts at once
  5. A push looks like a tilt
  6. Change the range
Tilt from the accelerometerAn ADXL345 spirit level from the gallery: turn counts into degrees with asin and atan2, work out how many degrees its dead band is, and tighten it to one degree.50 min
  1. Run it
  2. Flat is level
  3. Which way to tip
  4. How wide is the band?
  5. atan2, not asin
  6. Tighten it to a degree

Materials

  • Accelerometer and gyroscope, MPU6050
  • Accelerometer, ADXL345
  • Arduino Uno R3
  • Half breadboard

Check for understanding

Each student finishes "Tilt from the accelerometer" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Tighten it to a degree".

Chapter 2. About 1 h 40 min

Turning and drifting

A gyro added up into an angle, the drift that comes with it, and the one-line filter that fixes it.

LessonTimeWhat students do
Gyro driftAdd up a gyroscope into an angle and watch it walk away: a real gyro’s 0.4 degrees a second of offset, added in the sketch. Predict when it is 2 degrees off, measure the offset at rest, subtract it, and see the gyro follow a real tilt.50 min
  1. Run it
  2. Watch it walk
  3. Rate times time
  4. Calibrate it out
  5. Follow a real tilt
  6. A worse gyro
The complementary filterOne line of code that keeps the gyro’s smoothness and the accelerometer’s honesty. Measure what is left of the drift, shove the board and see the filter shrug, follow a real tilt, and change its time constant.50 min
  1. Run it
  2. Where the drift went
  3. Shove it
  4. Follow a real tilt
  5. Change the time constant
  6. Pick a tau for a job

Materials

  • Accelerometer and gyroscope, MPU6050
  • Arduino Uno R3

Check for understanding

Each student finishes "The complementary filter" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Pick a tau for a job".

Chapter 3. About 1 h 40 min

North, steps and taps

A magnetometer compass and its traps, then a stream of readings turned into steps and taps.

LessonTimeWhat students do
Magnetometer headingAn LIS3MDL magnetometer and an LSM6DSOX from the gallery: a compass heading from atan2, the turn rate from the gyro, why tipping a compass throws it off by tens of degrees, and true north from declination.50 min
  1. Run it
  2. The field, as numbers
  3. Turning, from the gyro
  4. Tip the compass
  5. True north
  6. Hard and soft iron
Step and tap detectionTurn a stream of accelerometer numbers into events: a step counter with two thresholds and a lockout, cadence in steps a minute, and a tap and double-tap detector that looks for a jump between two samples.50 min
  1. Run it
  2. Ten steps
  3. Cadence
  4. A knock
  5. Double tap
  6. Count gentle steps

Materials

  • Accelerometer, ADXL345
  • Arduino Uno R3
  • Half breadboard
  • Magnetometer, LIS3MDL
  • Six-axis IMU, LSM6DSOX

Check for understanding

Each student finishes "Step and tap detection" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Count gentle steps".

Chapter 4. About 1 h

Boss build

A builder’s level and a compass that stays true when you tip it, on an MPU-9250.

LessonTimeWhat students do
Boss: a level and compassAn MPU-9250 on an Uno as a builder’s level with a green and a red LED, and a compass that stays right when you tip it: gravity gives roll and pitch, and they turn the magnetic field back to level before atan2.60 min
  1. Run it
  2. Level within a degree
  3. Off level
  4. A compass, flat
  5. Tip it, and it stays true
  6. True north for your town
  7. Make it yours

Materials

  • Arduino Uno R3
  • Half breadboard
  • LED x 2
  • Nine-axis IMU, MPU-9250
  • Resistor x 2

Check for understanding

Each student finishes "Boss: a level and compass" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.