0% found this document useful (0 votes)
8 views15 pages

RaspberryPi StudyNotes

The document provides a comprehensive overview of the Raspberry Pi 4B, detailing its ARM architecture, communication protocols, and project code for an animal detection and feeding system. Key features include the quad-core ARM Cortex-A72 processor, various communication protocols like I2C, SPI, and PWM, and important coding practices for hardware safety. It also highlights the importance of pull-up resistors and specific thresholds for AI detection and servo control.

Uploaded by

raitanya700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

RaspberryPi StudyNotes

The document provides a comprehensive overview of the Raspberry Pi 4B, detailing its ARM architecture, communication protocols, and project code for an animal detection and feeding system. Key features include the quad-core ARM Cortex-A72 processor, various communication protocols like I2C, SPI, and PWM, and important coding practices for hardware safety. It also highlights the importance of pull-up resistors and specific thresholds for AI detection and servo control.

Uploaded by

raitanya700
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Raspberry Pi 4B

Complete Study Notes


Architecture • Protocols • GPIO • PWM • I2C • Project Code
1. Raspberry Pi 4B Architecture

1.1 Overview
The Raspberry Pi 4B uses ARM architecture — specifically the Broadcom BCM2711 chip with a
quad-core ARM Cortex-A72 processor running at 1.5–1.8 GHz.

Feature Detail
Chip Broadcom BCM2711
CPU Quad-core ARM Cortex-A72
Architecture ARMv8-A (64-bit)
Speed 1.5 GHz (overclockable to ~1.8 GHz)
Cache 32KB L1 + 1MB L2
Pipeline 15-stage out-of-order execution

1.2 What is ARM?


ARM = Advanced RISC Machine. It is a family of RISC-based processor architectures. ARM
doesn't manufacture chips — they design the architecture and license it to others like
Broadcom, Apple, Qualcomm, etc.

1.3 ARM Across Raspberry Pi Generations


Model Processor Architecture
Pi 1 ARM1176JZF-S ARMv6 (32-bit)
Pi 2 Cortex-A7 ARMv7 (32-bit)
Pi 3 Cortex-A53 ARMv8 (64-bit)
Pi 4B Cortex-A72 ARMv8-A (64-bit)
Pi 5 Cortex-A76 ARMv8.2-A (64-bit)

1.4 RISC-V vs ARM


The Raspberry Pi 4B uses ARM, not RISC-V. However ARM itself is based on RISC principles.
RISC-V is open-source and free, while ARM is proprietary.
Feature ARM (Pi 4B) RISC-V
Type RISC-based RISC-based
License Proprietary (ARM Ltd) Open-source, free
Ecosystem Massive, mature Growing rapidly
Used in Pi 4B? Yes No

1.5 Can You Use RISC-V on Raspberry Pi 4B?


Not natively in hardware, but you can emulate or cross-compile:
• Emulate RISC-V using QEMU — great for learning the instruction set
• Cross-compile RISC-V assembly/C code on the Pi using riscv64 toolchain
• For real RISC-V hardware: SiFive HiFive, StarFive VisionFive 2, Milk-V Pi
2. Communication Protocols

A communication protocol is a set of rules that defines how two or more devices send and
receive data with each other.

2.1 I2C — Inter-Integrated Circuit


Used in the project for the MLX90640 thermal camera.
Feature Detail
Wires 2 (SDA + SCL)
Speed 100kHz / 400kHz / 1MHz
Devices Up to 127 on same bus
Type Serial, Synchronous
Best For Sensors, displays, thermal cameras

2.2 SDA and SCL Explained


Pin Full Name Role
SDA Serial Data Line Carries the data (sends and receives)
SCL Serial Clock Line Carries the clock signal (timing/sync)

Simple analogy: SCL is like a metronome — it ticks at a regular rate to keep everything in sync.
SDA is like a messenger — it carries the actual data in sync with each tick.

I2C on Raspberry Pi 4B
Pin Number Function
GPIO 2 (Pin 3) SDA (I2C1)
GPIO 3 (Pin 5) SCL (I2C1)

How Your Code Uses SDA & SCL


i2c = busio.I2C([Link], [Link], frequency=400000)
mlx = adafruit_mlx90640.MLX90640(i2c)

Part What it does


[Link] Refers to GPIO 3 (Pin 5) — clock line
[Link] Refers to GPIO 2 (Pin 3) — data line
frequency=400000 Sets I2C speed to 400kHz (fast mode)
MLX90640(i2c) Connects the sensor through those 2 wires

2.3 SPI — Serial Peripheral Interface


Feature Detail
Wires 4 (MOSI, MISO, SCLK, CS)
Speed Up to 100MHz
Type Serial, Synchronous
Best For SD cards, displays, ADC chips

Pin Full Name Role


MOSI Master Out Slave In Pi sends data
MISO Master In Slave Out Pi receives data
SCLK Serial Clock Clock signal
CS Chip Select Selects which device

2.4 UART — Universal Asynchronous Receiver Transmitter


Feature Detail
Wires 2 (TX + RX)
Speed 9600 to 115200 baud
Devices Only 2 (point to point)
Type Serial, Asynchronous (no clock line)
Best For GPS modules, Bluetooth, GSM modules

2.5 PWM — Pulse Width Modulation


Used in the project for servo motors.
Feature Detail
Wires 1 (signal)
Frequency 50Hz for servos
Type Analog-like digital signal
Controls Speed, angle, brightness

2.6 GPIO — General Purpose Input Output


Used in the project for the IR sensor.
Feature Detail
Wires 1 per pin
Type Digital (HIGH/LOW only)
Best For LEDs, buttons, IR sensors, relays

2.7 Other Protocols


Protocol Wires Speed Best For
1-Wire 1 ~16kbps Temperature sensors
(DS18B20)
CAN Bus 2 Up to 1Mbps Cars, industrial machines
USB 4 480Mbps Cameras, keyboards, storage
Ethernet 4 1Gbps Wired networking
WiFi 0 (wireless) 300Mbps+ IoT, remote communication
Bluetooth 0 (wireless) 3Mbps Short range wireless

2.8 Quick Comparison of All Protocols


Protocol Wires Speed Devices Used For
I2C 2 Medium 127 Sensors, displays
SPI 4 Fast Multiple SD cards, screens
UART 2 Medium 2 only GPS, Bluetooth
PWM 1 — 1 Servos, motors
GPIO 1 Fast 1 Buttons, LEDs
1-Wire 1 Slow Multiple Temperature
CAN 2 Fast 127 Cars, industry
USB 4 Very fast 127 Peripherals
2.9 Protocols Used in This Project
Protocol Used For
I2C MLX90640 thermal camera (SDA + SCL)
PWM 6 servo motors (3 gate + 3 medicine)
GPIO IR sensor (digital input)
USB Camera ([Link])

2.10 Easy Lines to Remember


Protocol Remember It As
I2C 2 wires, many sensors — SDA carries data, SCL beats the clock
PWM Pulse = position — longer pulse, bigger turn
GPIO Simply ON or OFF — like a light switch
USB Plug and play — just connect and the data flows
3. PWM and Servo Motors

3.1 What is PWM?


PWM = Pulse Width Modulation. It is a way of controlling devices by switching a signal ON and
OFF very rapidly — and changing how long it stays ON controls the servo position.

3.2 How a Servo Understands PWM


A servo motor listens for pulses sent 50 times per second (50Hz). The width of each pulse tells
the servo where to go.
Pulse Width Servo Angle
1ms 0°
1.5ms 90° (center)
2ms 180°

3.3 Duty Cycle


Duty cycle = percentage of time signal is ON in one period.
Duty Cycle Pulse Width Angle
2.5% 0.5ms 0°
7.5% 1.5ms 90°
12.5% 2ms 180°

3.4 The Formula


duty = angle / 18 + 2.5

Angle Calculation Duty Cycle


0° 0/18 + 2.5 2.5%
90° 90/18 + 2.5 7.5%
180° 180/18 + 2.5 12.5%
3.5 [Link] Library — Key Functions
Function Purpose
[Link]([Link]) Set physical pin numbering mode
[Link](pin, [Link]) Set pin as output
[Link](pin, 50) Create PWM object at 50Hz
[Link](0) Start PWM signal
[Link](dc) Move servo to angle
[Link](0) Stop signal — prevents jitter
[Link]() Fully stop PWM
[Link]() Reset all GPIO pins

3.6 Why Set Duty Cycle to 0 After Moving?


• After reaching position, PWM signal is cut off completely
• Prevents jitter — servo stops shaking/humming
• Saves power — servo doesn't keep trying to hold position
4. Pull-up Resistors

4.1 What is a Pull-up Resistor?


A pull-up resistor connects a signal line to VCC through a resistor, ensuring the signal line is
pulled to a known HIGH voltage when nothing else is driving it.

4.2 Working Principle


Simple analogy: Think of it like a water tank on a rooftop. VCC = water tank (always full).
Resistor = pipe connecting tank to your tap. Signal line = your tap. When the tap is weak, the
tank tops it up through the pipe.

Without pull-up: GPIO outputs ~2.3V (weak) → Servo sees 2.3V — not enough.
With pull-up: VCC (3.3V) constantly pushes through resistor → Signal line rises to 3.3V — servo
works correctly.

4.3 Why a Resistor is Needed


• If you directly connect VCC to signal line without resistor — too much current flows
• GPIO pin could burn or short circuit
• Resistor limits the current while still pulling voltage up
• 4.7kΩ is recommended for servo signals

4.4 Wiring
3.3V Pin (Pi)
|
4.7kΩ
|
├──> Servo Signal Pin
|
Pi GPIO (PWM)

4.5 Resistor Values


Resistor Effect Recommendation
4.7kΩ Stronger pull-up Recommended for servos
10kΩ Weaker pull-up For I2C / input lines
4.6 One Line Summary
A pull-up resistor borrows voltage from VCC and adds it to your weak signal — like topping up a
half-filled glass with water from a full tank!
5. Project Code — Important Observations

This section covers the key observations from the animal detection and feeding system code
running on Raspberry Pi 4B.

5.1 GPIO Reset at Start


[Link]()
[Link]([Link])
Cleans any leftover GPIO state from a previous crashed run before setting up fresh. Prevents
pin conflicts.

5.2 IR Sensor Uses Built-in Pull-up


[Link](IR_SENSOR_PIN, [Link], pull_up_down=GPIO.PUD_UP)
Uses Pi's built-in pull-up — no external resistor needed for IR sensor. Signal reads LOW when
triggered (active low logic).

5.3 Servo Signal Killed After Moving


servos[servo_key].ChangeDutyCycle(0)
After reaching position, PWM signal is cut off completely — prevents jitter and saves power.
Very good practice.

5.4 Camera Warm-up Frames


for _ in range(5): [Link]()
ret, frame = [Link]()
First 5 frames are thrown away — camera needs time to adjust brightness and focus. The 6th
frame is the actual capture used for AI detection.

5.5 Confidence Threshold Gate


if output_data[idx] > CONFIDENCE_THRESHOLD: # 0.70
AI only acts if 70% confident — prevents wrong animal detection from triggering wrong food or
medicine. Critical safety check.

5.6 Medicine Servo Only Triggers on Fever


if has_fever:
move_servo(f"{animal}_med", 90)
Medicine dispensed only when temperature exceeds 39.2°C — food always dispenses but
medicine is conditional. Good safety logic.

5.7 Independent Timing for Food and Medicine


if med_open and elapsed >= MED_DURATIONS[animal]:
move_servo(f"{animal}_med", 0)
if gate_open and elapsed >= FEED_DURATIONS[animal]:
move_servo(f"{animal}_gate", 0)
Food and medicine servos close independently at different times per animal — they don't wait
for each other.

5.8 Waiting for Animal to Leave


waiting_for_clear = True
System won't process a new animal until the current one leaves the IR sensor zone — prevents
double triggering.

5.9 Graceful Cleanup on Exit


finally:
for s in [Link]():
if s: [Link]()
[Link]()
Even if the code crashes, all servos stop and GPIO resets — prevents servos from staying
stuck open. Very important for hardware safety.

5.10 Summary Table


Observation Why It Matters
GPIO reset at start Prevents pin conflicts
Built-in pull-up on IR No extra resistor needed
PWM killed after move No jitter, saves power
Camera warm-up frames Better image quality
70% confidence threshold Prevents wrong detection
Medicine only on fever Safety logic
Independent servo timing Flexible control per animal
Wait for animal to leave No double triggering
Finally block cleanup Hardware safety on crash
6. Quick Revision — One Liners

Topic Remember As
ARM Architecture Pi 4B uses ARMv8-A 64-bit — fast, efficient, RISC-based
RISC-V on Pi Not native — emulate with QEMU or use dedicated RISC-V
board
I2C 2 wires (SDA + SCL), up to 127 devices, used for thermal
camera
SDA Serial Data Line — carries the actual data
SCL Serial Clock Line — sets the timing/rhythm
SPI 4 wires, very fast, used for SD cards and displays
UART 2 wires, point-to-point only, used for GPS and Bluetooth
PWM Pulse width = angle — longer pulse means bigger rotation
GPIO Simply HIGH or LOW — on or off, nothing in between
Pull-up Resistor Borrows from VCC to top up a weak signal
4.7kΩ Best pull-up value for servo signal lines
Fever Threshold 39.2°C — above this, medicine servo activates
Confidence Threshold 70% — AI must be this sure before acting
Camera Warm-up Discard first 5 frames for better image quality
GPIO Cleanup Always run at end — resets all pins safely

You might also like