Raspberry Pi 4 Model B (4GB RAM) Documentation
The Raspberry Pi 4 Model B is a single-board computer developed by the Raspberry Pi Foundation. It is a powerful, compact, and highly affordable device that can be used for a wide range of applications, from DIY projects to industrial automation. The 4GB RAM variant provides a significant performance boost, making it suitable for more demanding tasks.
CPU: Broadcom BCM2711B0 quad-core Cortex-A72 (ARMv8) 64-bit SoC
RAM: 4GB LPDDR4-2400 SDRAM
Storage: MicroSD card slot (supports up to 1TB)
Operating System: Raspberry Pi OS (based on Linux)
Networking: Dual-band 802.11ac wireless LAN, Bluetooth 5.0, Gigabit Ethernet, and 2 USB 3.0 ports
GPIO: 40-pin GPIO header with 26 GPIO pins, 2 I2C, 2 SPI, and 1 UART
### Example 1: Simple Python Script to Control an LED using GPIO
In this example, we will use the Raspberry Pi's GPIO pins to control an LED. We will create a Python script that blinks an LED connected to pin 17.
Raspberry Pi 4 Model B (4GB RAM)
LED
1k resistor
Breadboard
Jumper wires
Raspberry Pi OS (latest version)
Python 3.x (pre-installed)
Code
```python
import RPi.GPIO as GPIO
import time
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Set up GPIO pin 17 as an output
GPIO.setup(17, GPIO.OUT)
try:
while True:
# Turn on the LED
GPIO.output(17, GPIO.HIGH)
time.sleep(1)
# Turn off the LED
GPIO.output(17, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
# Clean up GPIO on exit
GPIO.cleanup()
```
Explanation
In this example, we import the RPi.GPIO library, which provides a Python interface to the Raspberry Pi's GPIO pins. We set up pin 17 as an output, then use a while loop to toggle the LED on and off using the `GPIO.output()` function. The `time.sleep()` function is used to introduce a delay between each toggle.
### Example 2: Web Server using Flask to Control a Relay Module
In this example, we will create a web server using Flask to control a relay module connected to the Raspberry Pi's GPIO pins.
Raspberry Pi 4 Model B (4GB RAM)
Relay module (e.g., SRD-05VDC-SL-C)
Breadboard
Jumper wires
Raspberry Pi OS (latest version)
Python 3.x (pre-installed)
Flask (install using `pip install flask`)
Code
```python
from flask import Flask, request
import RPi.GPIO as GPIO
# Set up GPIO mode
GPIO.setmode(GPIO.BCM)
# Set up GPIO pin 18 as an output for the relay module
GPIO.setup(18, GPIO.OUT)
@app.route('/relay', methods=['POST'])
def relay_control():
state = request.form['state']
if state == 'on':
GPIO.output(18, GPIO.HIGH)
elif state == 'off':
GPIO.output(18, GPIO.LOW)
return 'Relay set to ' + state
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
```
Explanation
In this example, we create a Flask web server that listens for POST requests to the `/relay` endpoint. The request body contains the state of the relay (either 'on' or 'off'). We use the `GPIO.output()` function to set the relay state accordingly.
To test this example, open a web browser and navigate to `http://raspberrypi_IP_address:5000/relay` (replace `raspberrypi_IP_address` with your Raspberry Pi's IP address). Use a tool like `curl` or a POST request tool to send a POST request with the `state` parameter set to 'on' or 'off'.
For more information on using the Raspberry Pi 4 Model B (4GB RAM) and its GPIO pins, refer to the official Raspberry Pi documentation and resources:
Official Raspberry Pi Documentation: <https://www.raspberrypi.org/documentation/>
Raspberry Pi GPIO Library: <https://gpiozero.readthedocs.io/en/stable/>
Flask Web Framework: <https://flask.palletsprojects.com/>