Use ← → to move, Home / End to jump, and F for fullscreen.
Figure descriptions
Slide 2 · Part 1 · The Pi stops talking and starts listening
Overview. On the left, Labs 3 to 5, where the Raspberry Pi drives outputs: an LED (a pin high or low), a MOSFET and DC motor (PWM duty), and a stepper or servo (coil patterns, pulse width). On the right, Labs 6 and 7, where the Pi listens to inputs: the MCP9808 (an I2C register converted to degrees Celsius), the LIS3DH (I2C registers converted to x, y, z in g) and the HC-SR04 (a pulse width converted to distance).
Slide 3 · Part 2 · Inside an accelerometer: a mass on springs
Sketch of a MEMS accelerometer comb. A proof mass on springs between two anchors carries fingers that sit between fixed electrodes. At rest the two gaps beside each finger are equal, so C1 equals C2. When the chip accelerates to the right the mass lags to the left: the gap on one side shrinks, so C1 rises, and the other grows, so C2 falls.
Slide 4 · At rest, the axis pointing up reads +1 g
Side views of a breakout in four orientations with its x and z axes and gravity pointing down. Flat with the chip up it reads x 0, y 0, z +1 g; upside down z reads -1 g; with the x axis pointing up x reads +1 g; with x pointing down x reads -1 g. At rest the axis pointing up reads +1 g because the support pushes the chip up.
Slide 5 · Tilt: gravity shared between two axes
A board tilted by theta on a slope. The +1 g reading points straight up and is split into a component along the board, a_x = g sin theta, and one normal to the board, a_z = g cos theta. At theta = 30 degrees, a_x = 0.500 g and a_z = 0.866 g.
Slide 6 · 12 bits of data in a 16-bit register
The 16 bits of one LIS3DH axis: OUT_X_H at 0x29 holds 0x3D and OUT_X_L at 0x28 holds 0x40, giving 0011 1101 0100 0000 with bit 15 as the sign. Below, the bits each mode fills from the top: low-power 8 bits at 16 mg per digit, normal 10 bits at 4 mg per digit, high resolution 12 bits at 1 mg per digit. Data are left-justified and the unused low bits read 0.
Slide 7 · Inside the LIS3DH: sensor, ADC, registers
LIS3DH datasheet Figure 1, block diagram: the X, Y and Z sensing capacitors feed a multiplexer, charge amplifier and A/D converter into control logic with I2C and SPI interfaces. A second A/D converter serves three auxiliary ADC inputs and a temperature sensor. Blocks for self-test, reference, trimming circuits, clock, a 32-level FIFO and interrupt generation sit below.
LIS3DH datasheet Figure 2, top view: the package with pin 1 marked, and arrows showing the X, Y and Z directions of the detectable accelerations.
Slide 8 · Wiring the breakout to the Pi
Wiring an LIS3DH breakout to the Pi header. Pin 1 (3V3) goes to Vin, pin 3 (SDA) to SDA, pin 5 (SCL) to SCL and pin 6 (GND) to GND; the 5 V pins 2 and 4 are not used. SDO sets the address: open gives 0x18, tied to 3V3 gives 0x19. 3Vo, CS, INT and A1 to A3 stay open for I2C. Pins should be matched by their printed labels, because the header order differs between board versions.
Slide 9 · The driver module: registers behind method names
Five software and hardware layers. The application tilt_log.py calls sensor.getX() and knows the experiment. The driver LIS3DHMOD.py reads registers 0x28 and 0x29 and converts to g; it knows the chip. The smbus2 library calls read_byte_data(0x18, 0x28) and knows I2C transactions. The Linux kernel device /dev/i2c-1 sends START, address, ACK and STOP and drives SDA and SCL. The LIS3DH registers, such as 0x0F ID, 0x20 CTRL_REG1 and 0x28 OUT_X_L, make the numbers. Requests go down and data come back up.
Slide 10 · Two control registers set up the whole chip
Bit fields after the driver's defaults and setDataRate(0b0101). CTRL_REG1 at 0x20: ODR 0101, LPen 0, Zen Yen Xen 1 1 1, which is 0x57: 100 Hz, not low-power, all axes on. CTRL_REG4 at 0x23: BDU 1, BLE 0, FS 00, HR 1, ST 00, SIM 0, which is 0x88: block data update, plus or minus 2 g, 12-bit high resolution.
Slide 11 · Driver script vs. application script
Two ways to organize code. Left: one long script that mixes register addresses, bit masks, plotting, CSV writing, two's complement, a divisor and experiment loops, and gets copied into every lab. Right: one driver file, LIS3DHMOD.py, holding registers, bits and scaling, imported by three small applications (check_axes.py, tilt_log.py, pendulum.py). The driver is tested once and every experiment reuses it.
Slide 15 · Data strategy 1: plot while you record
Simulated accelerometer data over 10 seconds: z starts at 1 g and x at 0; a 30 degree tilt moves x to 0.5 g and z to 0.87 g, then back; a shake near 8 seconds makes x and y oscillate. The last 5 seconds are shaded as the window a live plot would show.
Slide 17 · Worked example: register bytes to g
Converting two register bytes to g in five steps. H = 0x3D and L = 0x40 combine to 0x3D40 = +15680, which divided by 16 is +980 counts, which at 1 mg per digit is +0.980 g; the driver does this in one step as 15680 / 16000. H = 0xE0 and L = 0xC0 combine to 0xE0C0; the top bit is 1, so subtracting 65536 gives -8000, then -500 counts and -0.500 g, or -8000 / 16000 in one step.
Slide 18 · Noise sets the smallest tilt you can see
Accelerometer tilt response ax/g equals sine theta, while angular sensitivity is proportional to cosine theta. Sensitivity is greatest near a horizontal axis and tends to zero near 90 degrees.
Slide 19 · Pendulum: the sensor feels the string, not gravity
What each axis of a swinging accelerometer reads, computed for a 1 Hz swing of 0.2 rad (11.5 degrees). Left: a string pendulum at angle theta with the sensor at the bob; its radial axis points along the string toward the pivot and its tangential axis across it; the string's tension acts along the string, and gravity, drawn pointing down, is not sensed. Right, top: the angle swings between plus and minus 11.5 degrees at frequency f. Middle: the radial reading, g cos theta plus v squared over L, swings between 0.98 and 1.04 g, peaking at the bottom twice per swing, so at 2f. Bottom: the tangential reading is 0 for the string pendulum; for a sensor at the far end of a swinging rod it is minus one half g theta, plus and minus 0.1 g, at f.
Slide 20 · Challenge: write a tilt alarm
Simulated noisy tilt angle rising from about 10 degrees above 20 and back below 15. Dashed lines mark an arm threshold at 20 degrees and a rearm threshold at 15 degrees. Below, a simple 'angle above 20 degrees' test switches on and off many times as noise crosses the line, while an alarm with a 5-sample rule and a separate rearm threshold switches on once and off once.
Slide 22 · Part 3 · From the accelerometer to the range finder
HC-SR04 user guide cover: photos of the module front and back, and a sketch of the transmitter sending a chirp to a cylindrical target and receiving its echo, with a start pulse in and an echo time pulse out.
Slide 23 · Sound as a ruler
HC-SR04 user guide Figure 1: a transmitter sends ultrasound toward a wall and the receiver hears the reflection, compared with a bat's echolocation; the distance between transmitter and wall is labeled.
Slide 24 · One pulse in, one pulse out
HC-SR04 user guide Figure 4, timing diagram: a trigger pulse of at least 10 microseconds, an 8-cycle sonic burst from the module, and an echo pulse of 100 microseconds to 18 ms, about 36 ms if no object is detected; allow 10 ms from the end of the echo to the next trigger.
Slide 25 · Pins and voltage: which HC-SR04 do you have?
HC-SR04 user guide pin drawing and table: pins VCC (3.3 to 5 V), TRIG (triggering input, 10 microsecond TTL pulses), ECHO (TTL logic output proportional to distance) and GND.
Slide 26 · Echo at 5 V, GPIO at 3.3 V: a divider
HC-SR04 Echo voltage divider: the 5 V Echo output feeds a resistor divider, and its midpoint feeds a Raspberry Pi GPIO input. The lower resistor returns to common ground, reducing the input voltage to a safe level.
Slide 27 · From echo time to distance
Distance against echo pulse width, d = v t / 2 with v = 343.2 m/s at 20 degrees Celsius: a straight line through the origin. Marked points: 1 m at 5.8 ms, 2 m at 11.7 ms and 4 m at 23.3 ms. The timing diagram's echo range of 100 microseconds to 18 ms is shaded, and about 36 ms is marked as the no-object pulse.
Slide 28 · Pulse timing: from microseconds to Python
HC-SR04 timing, not to scale, annotated with Python. Trig: a 10 microsecond pulse from GPIO.output(TRIG, 1), sleep(10e-6), GPIO.output(TRIG, 0). Sound: a burst of 8 cycles at 40 kHz. Echo: goes high and later low; its width is the round-trip time. A loop 'while ECHO reads 0' records start at the rising edge and a loop 'while ECHO reads 1' records end at the falling edge; t = end minus start, and no edge within the timeout returns None.
Slide 30 · Lab 6 meets Lab 7: correct the speed of sound
Speed of sound against air temperature from 0 to 40 degrees Celsius: the exact form 331.3 times the square root of 1 + T/273.15 and the linear approximation 331.3 + 0.606 T nearly coincide. Marked values: 337.3 m/s at 10 degrees, 343.2 at 20 and 349.0 at 30. Assuming 20 degrees in 30 degree air makes the distance 1.7 percent short, 17 mm at 1 m.
Slide 31 · Two sensors, one Pi
Block wiring for Lab 7 Task 7. The Pi's pin 1 (3V3), pin 3 (SDA) and pin 5 (SCL) form an I2C bus shared by an MCP9808 at address 0x18 (A0 to A2 open) and an LIS3DH at 0x19 (SDO to 3V3). Pin 2 (5 V) powers the HC-SR04; pin 16 (GPIO 23) drives Trig; Echo returns through a 5 V to 3.3 V divider to pin 18 (GPIO 24). All grounds are joined.
























