raspberry pi

How to use led with Arduino and Raspberry pi, for beginners

Description:

Led With Arduino and Raspberry pi– I have been using Arduino and Raspberry pi in beginners level, intermediate-level, and advanced-level projects. in this article, I am going to show you the use of led with two different controller boards, one is Arduino and the other is raspberry pi with circuit diagrams and programs explanation.


Amazon Links:

Arduino Uno

Raspberry Pi

raspberry pi 4 4gb kit

Wireless Keyboard and Mouse for raspberry pi:

Night vision Camera for Raspberry Pi:

Oled HDMI touch display for raspberry pi:

TOP10 Best Raspberry Pi Kits

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!



Using led with Arduino and Raspberry pi:

In the Arduino world, almost every beginner starts with flashing the LED. In this first experiment, you will first control an LED using an Arduino board and then from a Raspberry Pi. This is an easy project intended for beginners. Only two components need to be connected to the test board: an LED and a resistor. All LEDs require a resistor in order to limit the current flowing through them. Components Needed Whether you are using a Raspberry Pi or an Arduino (or both), you will need the following components to perform the experiment.

The following component will be needed for making this mini-project:

Arduino Uno

Yellow led (any)

330 ohm resistor

Connection wires


Building the Circuit

The circuit for this project is shown in Figure 1. The Led connections are the same whether you are using an Arduino or a Raspberry Pi, Only the pins are different to which the LED is connected.

LED with Arduino and raspberry pi
Figure 1

Figure1 shows that the current passed through an output pin of an Arduino or Raspberry Pi first passes through the resistor before reaching the LED and turning it on. The direction of current flow in the resistor does not matter, but the positive leg of the LED must be directed towards the top of the board. The positive leg of an LED is slightly longer than the negative or GND leg. In addition, the LED has a flat face on the negative leg side.


Arduino Led Circuit:

Connect the GND leg of the LED with the GND pin of the Arduino, and connect the positive leg of the LED with the digital pin D9 of the Arduino as shown in Figure2.

LED with Arduino and raspberry pi
Figure 2

Arduino Led program

the program turns the LED on for 5 seconds, then turns it off for 2 seconds, and then this sequence is repeated again and again. Here is the full code:

const int controlPin = 9;
void setup() {
  // put your setup code here, to run once:
  pinMode(controlPin, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(controlPin, HIGH);
  delay(5000);
  digitalWrite(controlPin,LOW);
  delay(2000);

}


Arduino Led Code Explanation:

const int controlPin =9;

The first line defines a constant named controlPin as pin 9. Although the Arduino Uno has digital pins numbered 0 through 13 and analog pins numbered 0 through 5, when the Arduino code refers to a number only ( here, 9), usage dictates that it is a digital pin. To designate one of the six analog pins, you must precede the pin number with the letter A.

void setup() {

  // put your setup code here, to run once:

  pinMode(controlPin, OUTPUT);




}

The setup() function sets the pin to be a digital output using pinMode.

void loop() {

  // put your main code here, to run repeatedly:

  digitalWrite(controlPin, HIGH);

  delay(5000);

  digitalWrite(controlPin,LOW);

  delay(2000);

The loop() function which is repeated endlessly first sets the high level (5V) for controlPin (9) in order to light the LED. It then imposes a delay of 5000 milliseconds (5 seconds) before setting controlPin to the low level (0V) in order to turn off the LED. The next line imposes a new delay of 2 seconds before starting the loop again from the beginning.



Led With Raspberry Pi

Unlike the pins on the Arduino board, the GPIO connectors on the Raspberry Pi are not labeled. You have two possibilities: you can refer to a GPIO pinout diagram to find the required pin; or, you can use a pin identification jig that you place on the GPIO connector.

The following component will be needed for making this mini project in raspberry pi:

Raspberry pi

Yellow led (any)

330 ohm resistor

Connection wires

Raspberry pi Led Circuit diagram:

LED with Arduino and raspberry pi
Figure 3


Raspberry Pi Led program

No need to go through another computer to program the Raspberry Pi – you can write and run the below program directly on this nano-computer:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
control_pin=18
GPIO.setup(control_pin, GPIO.OUT)

try:
    while True:
        
        GPIO.output(control_pin, False)
        time.sleep(5)
        GPIO.output(control_pin, True)
        time.sleep(2)
finally:
    GPIO.cleanup()

Save the above code as with the name on_off_control.py


Raspberry pi Led Code explanation:

The program looks a lot like the Arduino sketch:

import RPi.GPIO as GPIO

To access the GPIO pins of the Raspberry Pi, we use a Python library called RPi.GPIO was created by Raspberry Pi enthusiast Ben Croston. The first line of code imports this library so that it can be used in the program. The RPi.GPIO library is preinstalled with all recent versions of the standard Raspbian distribution. So you don’t need to install it unless you are using an older version of Raspbian. In this case, it is better to update your system by entering the following command in the terminal: $ Sudo apt-get upgrade

import time

The time library must also be imported because it allows defining the activation and deactivation times of the led.

GPIO.setmode(GPIO.BCM)

The GPIO.setmode line (GPIO.BCM) must be included in all Python programs used to control GPIO pins before setting the mode of the pins or using them in any way. The command tells the GPIO library that pins should be identified by their Broadcom name (BCM) rather than their position. The RPi.GPIO library supports both naming methods, but the Broadcom convention is the most popular. There is no separate setup() and loop() functions like in Arduino programming. Instead, whatever would be defined in the setup () function simply appears at the start of the program, and endlessly executes, while the loop takes care of everything that would normally be in the loop () function in Arduino.

control_pin=18

The control_pin variable designates GPIO pin 18 as the pin used to drive the LED. Then this pin is set as an output using GPIO. setup.


try:

 Here we come to the equivalent of the loop function in Arduino. The statements are contained in a try / finally construct. Thus, in the event of an error in the execution of the program or of its execution being interrupted by entering the Ctrl-C shortcut in the terminal window used for the execution of the program, the cleanup code contained in the finally block will be executed. You could omit this code and settle for the single while loop, but the cleanup code automatically redefines all GPIO pins as inputs, which limits the chances of accidental short circuit or breadboard wiring error that would damage the Raspberry. Pi.

while True:

The while loop has the raw condition. It might sound strange to you, but that’s how we define endless code execution in Python. The program loops through the commands defined inside the while loop until you interrupt it by entering Ctrl-C or unplugging the Raspberry Pi.

        GPIO.output(control_pin, False)

        time.sleep(5)

        GPIO.output(control_pin, True)

        time.sleep(2)

Inside the loop, the code looks a lot like the Arduino equivalent. The GPIO pin is set to True (high level), then a 5-second delay is applied before the GPIO pin is set to False (low level). Another 2-second delay then occurs before the cycle begins again.



Raspberry Pi Led Code testing:

To access the GPIO pins, you must have Linux superuser privileges. Open the folder containing the on_off_control.py file, then start running the program using the following command:

$ sudo python on_off_control.py

When you type sudo at the start of the command, you start execution as a superuser. To stop the blinking of the led, use the shortcut Ctrl-C to exit the program.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button