.. note::

    Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

    **Why Join?**

    - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
    - **Learn & Share**: Exchange tips and tutorials to enhance your skills.
    - **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
    - **Special Discounts**: Enjoy exclusive discounts on our newest products.
    - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.

    👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!

.. _1.1.1_py:

1.1.1 Blinking LED
=========================

Introduction
-----------------

In this project, we will learn how to make a blinking LED by programming.
Through your settings, your LED can produce a series of interesting
phenomena. Now, go for it.

Required Components
------------------------------

In this project, we need the following components. 

.. image:: ../img/blinking_led_list.png
    :width: 800
    :align: center

It's definitely convenient to buy a whole kit, here's the link: 

.. list-table::
    :widths: 20 20 20
    :header-rows: 1

    *   - Name	
        - ITEMS IN THIS KIT
        - LINK
    *   - Raphael Kit
        - 337
        - |link_Raphael_kit|

You can also buy them separately from the links below.

.. list-table::
    :widths: 30 20
    :header-rows: 1

    *   - COMPONENT INTRODUCTION
        - PURCHASE LINK

    *   - :ref:`cpn_gpio_extension_board`
        - |link_gpio_board_buy|
    *   - :ref:`cpn_breadboard`
        - |link_breadboard_buy|
    *   - :ref:`cpn_wires`
        - |link_wires_buy|
    *   - :ref:`cpn_resistor`
        - |link_resistor_buy|
    *   - :ref:`cpn_led`
        - |link_led_buy|


Schematic Diagram
---------------------

In this experiment, connect a 220Ω resistor to the anode (the long pin
of the LED), then the resistor to 3.3 V, and connect the cathode (the
short pin) of the LED to GPIO17 of Raspberry Pi. Therefore, to turn on
an LED, we need to make GPIO17 low (0V) level. We can get this
phenomenon by programming.

.. note::

    **Pin11** refers to the 11th pin of the Raspberry Pi from left to right, and its corresponding **wiringPi** and **BCM** pin numbers are shown in the following table.

In the C language related content, we make GPIO0 equivalent to 0 in the
wiringPi. Among the Python language related content, BCM 17 is 17 in the
BCM column of the following table. At the same time, they are the same
as the 11th pin on the Raspberry Pi, Pin 11.

============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO17       Pin 11   0        17
============ ======== ======== ===

.. image:: ../img/image48.png
    :width: 800
    :align: center

Experimental Procedures
-----------------------------

**Step 1:** Build the circuit.

.. image:: ../img/image49.png
    :width: 800
    :align: center

**Step 2:** Go to the folder of the code and run it.

1. If you use a screen, you're recommended to take the following steps.

Find 1.1.1_BlinkingLed.py and double click it to open. Now you're in the
file.

Click **Run** ->\ **Run Module** in the window and the following
contents will appear.

To stop it from running, just click the X button on the top right to
close it and then you'll back to the code. If you modify the code,
before clicking **Run Module (F5)** you need to save it first. Then you
can see the results.

2. If you log into the Raspberry Pi remotely, type in the command:

.. raw:: html

   <run></run>

.. code-block::

   cd ~/raphael-kit/python

.. note::
    Change directory to the path of the code in this experiment via ``cd``.

**Step 3:** Run the code

.. raw:: html

   <run></run>

.. code-block::

   sudo python3 1.1.1_BlinkingLed.py

.. note::
    Here sudo - superuser do, and python means to run the file by Python.

After the code runs, you will see the LED flashing.

**Step 4:** If you want to edit the code file 1.1.1_BlinkingLed.py,
press ``Ctrl + C`` to stop running the code. Then type the following
command to open 1.1.1_BlinkingLed.py:

.. raw:: html

   <run></run>

.. code-block::

   nano 1.1.1_BlinkingLed.py

.. note::
    nano is a text editor tool. The command is used to open the
    code file 1.1.1_BlinkingLed.py by this tool.

Press ``Ctrl+X`` to exit. If you have modified the code, there will be a
prompt asking whether to save the changes or not. Type in ``Y`` (save)
or ``N`` (don’t save).

Then press ``Enter`` to exit. Type in nano 1.1.1_BlinkingLed.py again to
see the effect after the change.

**Code**

The following is the program code:

.. note::

   You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to  source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect.

.. raw:: html

    <run></run>

.. code-block:: python

   #!/usr/bin/env python3
   import RPi.GPIO as GPIO
   import time
   LedPin = 17
   def setup():
      # Set the GPIO modes to BCM Numbering
      GPIO.setmode(GPIO.BCM)
      # Set LedPin's mode to output,and initial level to High(3.3v)
      GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)
   # Define a main function for main process
   def main():
      while True:
         print ('...LED ON')
         # Turn on LED
         GPIO.output(LedPin, GPIO.LOW)
         time.sleep(0.5)
         print ('LED OFF...')
         # Turn off LED
         GPIO.output(LedPin, GPIO.HIGH)
         time.sleep(0.5)
   # Define a destroy function for clean up everything after the script finished
   def destroy():
      # Turn off LED
      GPIO.output(LedPin, GPIO.HIGH)
      # Release resource
      GPIO.cleanup()                   
   # If run this script directly, do:
   if __name__ == '__main__':
      setup()
      try:
         main()
      # When 'Ctrl+C' is pressed, the program destroy() will be  executed.
      except KeyboardInterrupt:
         destroy()

**Code Explanation**

.. code-block:: python

   #!/usr/bin/env python3

When the system detects this, it will search the installation path of
python in the env setting, then call the corresponding interpreter to
complete the operation. It’s to prevent the user not installing the
python onto the ``/usr/bin`` default path.

.. code-block:: python

   import RPi.GPIO as GPIO

In this way, import the RPi.GPIO library, then define a variable, GPIO
to replace RPI.GPIO in the following code.

.. code-block:: python

   import time

Import time package, for time delay function in the following program.

.. code-block:: python

   LedPin = 17

LED connects to the GPIO17 of the T-shape extension board, namely, BCM
17.

.. code-block:: python

   def setup():
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(LedPin, GPIO.OUT, initial=GPIO.HIGH)

Set LedPin's mode to output, and initial level to High (3.3v).

There are two ways of numbering the IO pins on a Raspberry Pi within
RPi.GPIO: BOARD numbers and BCM numbers. In our projects, what we use is
BCM numbers. You need to set up every channel you are using as an input
or an output.

.. code-block:: python

   GPIO.output(LedPin, GPIO.LOW)

Set GPIO17(BCM17) as 0V (low level). Since the cathode of LED is
connected to GPIO17, thus the LED will light up.

.. code-block:: python

   time.sleep(0.5)

Delay for 0.5 second. Here, the statement is delay function in C language, the unit is second.

.. code-block:: python

   def destroy():
      GPIO.cleanup()  

Define a destroy function for clean up everything after the script
finished.

.. code-block:: python

   if __name__ == '__main__':
      setup()
      try:
         main()
      # When 'Ctrl+C' is pressed, the program destroy() will be  executed.
      except KeyboardInterrupt:
         destroy()

This is the general running structure of the code. When the program
starts to run, it initializes the pin by running the setup(), and then
runs the code in the main() function to set the pin to high and low
levels. When ``Ctrl+C`` is pressed, the program,
destroy() will be  executed.  

Phenomenon Picture
--------------------

.. image:: ../img/image54.jpeg
    :width: 800
    :align: center