nielit-rpi)
The nielit-raspberrypi-practicals library provides a unified, object-oriented, and student-friendly Python framework covering all 20 official NIELIT Raspberry Pi practicals (Practicals 3.1 through 3.20).
Each practical is accompanied by:
with statements) and automatic cleanup of GPIO pins to protect hardware.nielit-rpi command-line utility lets students inspect diagnostics, browse practicals, and run or export experiments instantly.Installs core GPIO and basic sensor support:
pip install nielit-raspberrypi-practicals
Installs all peripheral drivers including I2C LCD (smbus2), SPI RFID (mfrc522, spidev), DHT sensors (adafruit-circuitpython-dht), MQTT (paho-mqtt), and Web server (flask):
pip install "nielit-raspberrypi-practicals[all]"
On newer Raspberry Pi OS releases (Debian Bookworm), Python enforces PEP 668 (externally managed environment). Use a virtual environment:
# Create a virtual environment with access to system packages
python3 -m venv ~/nielit-env --system-site-packages
# Activate the virtual environment
source ~/nielit-env/bin/activate
# Install package
pip install --upgrade "nielit-raspberrypi-practicals[all]"
Tip: Add
source ~/nielit-env/bin/activateto your~/.bashrcto activate the environment automatically upon terminal launch.
Ensure that I2C, SPI, and Serial interfaces are enabled on your Raspberry Pi:
# Enable I2C interface
sudo raspi-config nonint do_i2c 0
# Enable SPI interface
sudo raspi-config nonint do_spi 0
# Enable Serial UART
sudo raspi-config nonint do_serial_hw 0
nielit-rpi)The package includes a powerful command-line tool named nielit-rpi.
Display a formatted table of all 20 practicals with their hardware requirements and difficulty ratings:
nielit-rpi list
Output Preview:
ID | Title | Hardware | Difficulty
--------------------------------------------------------------------------------
3_1 | System Information | RPi only | Beginner
3_2 | GPIO LED Output | LED + resistor | Beginner
3_3 | Push Button Input | Push button | Beginner
3_4 | Button Controlled LED | Button + LED | Beginner
3_5 | Traffic Light Controller | 3 LEDs | Beginner
3_6 | PWM LED Brightness | LED | Beginner
...
3_20 | Smart Home Capstone | PIR + LED | Advanced
Run an automated diagnostic check on your Raspberry Pi system, kernel, Python environment, and enabled hardware interfaces (I2C, SPI):
nielit-rpi check
Output Preview:
--- System Check ---
Hostname: raspberrypi
Python version: 3.11.2
Model: Raspberry Pi 4 Model B Rev 1.4
--- Dependencies ---
[OK] gpiozero is installed
[OK] smbus2 is installed
[OK] paho-mqtt is installed
[OK] flask is installed
--- Hardware Interfaces ---
[OK] I2C interface is enabled (/dev/i2c-1 found)
[OK] SPI interface is enabled (/dev/spidev0.0 found)
Retrieve the aim, hardware components, circuit wiring, and safety notes for any practical:
nielit-rpi info 3_2
nielit-rpi info 3_10
Execute any practical directly by its ID from any folder:
# Run system diagnostics
nielit-rpi run 3_1
# Run LED blinking practical
nielit-rpi run 3_2
# Run DHT11 temperature & humidity monitoring
nielit-rpi run 3_10
Export all 20 editable practical scripts into your current project folder:
nielit-rpi export-examples
Or specify a custom directory:
nielit-rpi export-examples --dest my_practicals
Students can then navigate into any folder and run or modify main.py:
cd examples/practical_3_2
python main.py
| ID | Practical Title | Hardware Required | BCM Pins | Category | Difficulty | CLI Command |
|---|---|---|---|---|---|---|
| 3_1 | System Diagnostics & Info | Raspberry Pi only | โ | System | Beginner | nielit-rpi run 3_1 |
| 3_2 | GPIO LED Output | LED, 330ฮฉ Resistor | GPIO 17 | GPIO Output | Beginner | nielit-rpi run 3_2 |
| 3_3 | Push Button Input | Push Button | GPIO 18 | GPIO Input | Beginner | nielit-rpi run 3_3 |
| 3_4 | Button-Controlled LED | LED, Button, 330ฮฉ Resistor | GPIO 17, 18 | Interactive | Beginner | nielit-rpi run 3_4 |
| 3_5 | Traffic Light Controller | Red, Yellow, Green LEDs | GPIO 17, 27, 22 | State Machine | Beginner | nielit-rpi run 3_5 |
| 3_6 | PWM LED Brightness | LED, 330ฮฉ Resistor | GPIO 18 (PWM) | PWM Analog | Beginner | nielit-rpi run 3_6 |
| 3_7 | Active Buzzer Alert | Active Buzzer | GPIO 23 | Audio Alert | Beginner | nielit-rpi run 3_7 |
| 3_8 | Servo Motor Angle Control | SG90 Micro Servo, Ext 5V | GPIO 18 (PWM) | Actuators | Intermediate | nielit-rpi run 3_8 |
| 3_9 | HC-SR04 Ultrasonic Distance | HC-SR04, 1kฮฉ + 2kฮฉ Divider | GPIO 23, 24 | Sensors | Intermediate | nielit-rpi run 3_9 |
| 3_10 | DHT11 Temp & Humidity | DHT11 Sensor, 10kฮฉ Pull-up | GPIO 4 | Sensors | Intermediate | nielit-rpi run 3_10 |
| 3_11 | I2C 16x2 LCD Display | 16x2 LCD with PCF8574 | GPIO 2 (SDA), 3 (SCL) | Displays | Intermediate | nielit-rpi run 3_11 |
| 3_12 | PIR Motion Detection | HC-SR501 PIR Sensor | GPIO 4 | Sensors | Intermediate | nielit-rpi run 3_12 |
| 3_13 | Relay Module Control | 5V Relay Module, AC Load | GPIO 26 | Actuators | Intermediate | nielit-rpi run 3_13 |
| 3_14 | LDR Light Sensor via ADC | LDR, 10kฮฉ Resistor, MCP3008 | SPI (GPIO 8..11) | ADC / SPI | Intermediate | nielit-rpi run 3_14 |
| 3_15 | MFRC522 RFID Card Reader | RC522 RFID Module, Cards | SPI (GPIO 8..11, 25) | RFID / SPI | Advanced | nielit-rpi run 3_15 |
| 3_16 | SQLite Sensor Data Logger | Raspberry Pi only | โ | Storage | Intermediate | nielit-rpi run 3_16 |
| 3_17 | Flask GPIO Web Controller | LED, LAN Network | GPIO 17 | Web / IoT | Advanced | nielit-rpi run 3_17 |
| 3_18 | MQTT Sensor Telemetry Pub | Network, MQTT Broker | โ | IoT / MQTT | Advanced | nielit-rpi run 3_18 |
| 3_19 | MQTT Remote LED Sub | LED, Network, MQTT Broker | GPIO 17 | IoT / MQTT | Advanced | nielit-rpi run 3_19 |
| 3_20 | Smart Home Energy Capstone | PIR Sensor, LED, Relay | GPIO 4, 17, 26 | Integrated | Advanced | nielit-rpi run 3_20 |
Import components into your own custom projects with import nielit_rpi:
nielit_rpi.gpio)from nielit_rpi.gpio import LEDController
import time
# Use as a context manager for automatic cleanup
with LEDController(pin=17) as led:
led.on()
time.sleep(1)
led.off()
led.blink(on_time=0.5, off_time=0.5, n=5)
from nielit_rpi.gpio import ButtonReader
btn = ButtonReader(pin=18, pull_up=True, bounce_time=0.05)
btn.when_pressed = lambda: print("Button Pressed!")
btn.when_released = lambda: print("Button Released!")
btn.wait_for_press(timeout=10)
btn.close()
from nielit_rpi.gpio import PWMLEDController, BuzzerController
# Fade LED
with PWMLEDController(pin=18) as pwm_led:
pwm_led.pulse(fade_in_time=1.0, fade_out_time=1.0, n=3)
# Buzzer alert
with BuzzerController(pin=23) as buzzer:
buzzer.beep(on_time=0.1, off_time=0.1, n=3)
nielit_rpi.sensors)from nielit_rpi.sensors import UltrasonicSensor
sensor = UltrasonicSensor(trigger_pin=23, echo_pin=24, max_distance=4.0)
print(f"Current Distance: {sensor.distance_cm:.2f} cm")
sensor.close()
import board
from nielit_rpi.sensors import DHTSensor
# Note: Uses Adafruit CircuitPython board pin definition
with DHTSensor(pin=board.D4, sensor_type="DHT11") as dht:
reading = dht.read()
print(f"Temperature: {reading['temperature']}ยฐC | Humidity: {reading['humidity']}%")
from nielit_rpi.sensors import PIRSensor
with PIRSensor(pin=4) as pir:
if pir.motion_detected:
print("Motion detected!")
from nielit_rpi.sensors import LDRSensor
with LDRSensor(channel=0) as ldr:
print(f"Raw Value: {ldr.value:.4f} | Voltage: {ldr.voltage:.2f}V")
nielit_rpi.actuators)from nielit_rpi.actuators import RelayController
with RelayController(pin=26, active_high=True) as relay:
relay.on() # Turn on connected load
relay.off() # Turn off load
relay.toggle()
from nielit_rpi.actuators import ServoController
with ServoController(pin=18, min_angle=-90, max_angle=90) as servo:
servo.angle = 0 # Center position
servo.angle = 90 # Maximum clockwise
servo.angle = -90 # Maximum counter-clockwise
servo.sweep(start=-90, end=90, step=15, delay=0.1)
nielit_rpi.displays)from nielit_rpi.displays import I2CLCD
with I2CLCD(address=0x27, bus_number=1) as lcd:
lcd.clear()
lcd.write_string("NIELIT Ropar", line=1)
lcd.write_string("IoT Laboratory", line=2)
nielit_rpi.communication)from nielit_rpi.communication import RFIDReader
with RFIDReader() as rfid:
print("Hold an RFID card near the reader...")
card_id, text = rfid.read()
print(f"Card UID: {card_id} | Data: {text}")
from nielit_rpi.communication import MQTTPublisher, MQTTSubscriber
# Publish reading
pub = MQTTPublisher(broker="broker.hivemq.com", port=1883, topic="nielit/rpi/sensor")
pub.connect()
pub.publish({"temperature": 25.4, "humidity": 60})
pub.disconnect()
# Subscribe to commands
def on_message(client, userdata, msg):
print(f"Received payload: {msg.payload.decode()}")
sub = MQTTSubscriber(broker="broker.hivemq.com", port=1883, topic="nielit/rpi/led", on_message_callback=on_message)
sub.connect()
sub.start()
nielit_rpi.storage)from nielit_rpi.storage import SensorDataLogger
logger = SensorDataLogger(db_path="telemetry.db", table_name="climate")
logger.create_table(columns={"temp": "REAL", "humidity": "REAL", "status": "TEXT"})
# Log reading with automatic timestamp
logger.log_reading(temp=24.5, humidity=58.2, status="OPTIMAL")
# Query recent records
rows = logger.get_readings(limit=10)
for row in rows:
print(row)
logger.close()
nielit_rpi.networking)from nielit_rpi.networking import create_gpio_web_app
app = create_gpio_web_app(led_pin=17)
# Run server on port 5000 accessible across the local network
app.run(host="0.0.0.0", port=5000, debug=False)
nielit_rpi.utilities)from nielit_rpi.utilities import get_system_info, cleanup_devices
# Fetch full system diagnostics dict
info = get_system_info()
print(f"RPi Model : {info.get('model')}")
print(f"Python : {info.get('python_version')}")
print(f"Disk Free : {info.get('disk_usage', {}).get('free_gb')} GB")
3.3V Power [01] [02] 5V Power
(I2C1 SDA) GPIO 2 / Pin 3 [03] [04] 5V Power
(I2C1 SCL) GPIO 3 / Pin 5 [05] [06] Ground
(GPCLK0) GPIO 4 / Pin 7 [07] [08] GPIO 14 / Pin 8 (UART TXD)
Ground / Pin 9 [09] [10] GPIO 15 / Pin 10 (UART RXD)
(SPI1_CE1) GPIO 17 / Pin 11 [11] [12] GPIO 18 / Pin 12 (PWM0)
GPIO 27 / Pin 13 [13] [14] Ground
GPIO 22 / Pin 15 [15] [16] GPIO 23 / Pin 16
3.3V Power [17] [18] GPIO 24 / Pin 18
(SPI0 MOSI) GPIO 10 / Pin 19 [19] [20] Ground
(SPI0 MISO) GPIO 9 / Pin 21 [21] [22] GPIO 25 / Pin 22
(SPI0 SCLK) GPIO 11 / Pin 23 [23] [24] GPIO 8 / Pin 24 (SPI0 CE0)
Ground / Pin 25 [25] [26] GPIO 7 / Pin 26 (SPI0 CE1)
(I2C0 ID_SD) GPIO 0 / Pin 27 [27] [28] GPIO 1 / Pin 28 (I2C0 ID_SC)
GPIO 5 / Pin 29 [29] [30] Ground
GPIO 6 / Pin 31 [31] [32] GPIO 12 / Pin 32 (PWM0)
GPIO 13 / Pin 33 [33] [34] Ground
(SPI1 MISO) GPIO 19 / Pin 35 [35] [36] GPIO 16 / Pin 36 (SPI1 CE2)
GPIO 26 / Pin 37 [37] [38] GPIO 20 / Pin 38 (SPI1 MOSI)
Ground / Pin 39 [39] [40] GPIO 21 / Pin 40 (SPI1 SCLK)
The package includes a comprehensive test suite with 100% hardware-mocked unit tests that run on Linux, Windows, macOS, and CI pipelines without requiring physical Raspberry Pi hardware.
# Clone the repository
git clone https://github.com/lovnishverma/nielit-raspberrypi-practicals.git
cd nielit-raspberrypi-practicals
# Install development dependencies
pip install -e ".[dev,all]"
# Run full pytest test suite
pytest tests/ -v
Permission denied: /dev/gpiomem or No access to /dev/memFix: Add your user to the gpio group and reboot:
sudo usermod -a -G gpio,i2c,spi $USER
sudo reboot
Fix:
i2cdetect -y 1 (default is usually 0x27 or 0x3F).RuntimeError: A full buffer was not returnedFix: DHT sensors use time-sensitive bit-banging. Occasional timeouts are normal. The DHTSensor.read() method in this library automatically retries up to 3 times. Ensure a 10kฮฉ pull-up resistor is placed between VCC and Data pin.
error: externally-managed-environment when running pip installFix: You are on Debian Bookworm. Create and activate a Python virtual environment as shown in the Installation Guide.
princelv84@gmail.com