Controlling GPIO pins of Raspberry Pi Zero

 

In the previous post, we saw how we can control a Raspberry Pi Zero over UART. We took control over the command prompt through UART. Now we can write a small python script for controlling the Raspberry Pi Zero’s GPIO pin, using the command prompt!!!. (Assuming we are connected to the Pi over UART as mentioned in the previous post.)

Ok, now we have the command prompt in our hand. At present, we are in the home directory. So move to the desktop by typing in the command.

todesktop

Now we are on the desktop directory of Pi Zero. Now let’s create a file for writing the python code. Type in as followscreate_file

When you press enter after typing the above command, a file named “led_blink” will be created in the Desktop directory. And we can type in the code for blinking an LED in the command window.

code

 

The above code can be used for blinking an LED. Here the pin number 4 refers to GPIO 4 of the Pi Zero (not 4th pin but 7th pin) as shown in the following pin map.

GPIO

The “time” library is used for setting a time delay. While writing code make sure the indentation is proper (since it is important in python).

After finishing the code press ctrl+X to exit from the text editor. Now the command prompt asks for the name in which the file to be saved. save_name

Press enter without changing anything as we have already given the name of the file. So once again we are back to the command window.

press_enter

You can check whether the file is present in the Desktop by typing the command “ls”. It lists all files and directories present in the Desktop (present folder).

check

Now we need to have an LED connected to the 4th pin before running the code. The connection is simple. What you need is an LED, two jumper wires, a bread board, and a small resistor. Connect them as shown

C360_2016-05-02-18-59-37-006_org

Connect the Red wire to the GPIO4 of Pi Zero and Black wire (to which the negative of the LED is connected) to the ground.

C360_2016-05-02-18-35-43-518

Now run the code by typing the following commands in the command window.

python

Tada!!!!!!!! The LED is blinking Now.

ezgif.com-video-to-gif

Leave a comment