.. _programPI: How to Program the Raspberry Pi =============================== In order to be able to create and express yourself with the Raspberry Pi, be it through connecting the Pi to the outside world through the GPIO pins or developing advanced data analysis tools, you will need to know the basics of how to program your device. Several languages of programming can be used on the RPi. This varies from simple visual programming to text and object based languages. Although visual programming such as **Scratch** is very intuitive and simple to use, text based languages such as **Python** are much more powerful and allow us to achieve much greater things. In later chapters you will learn how to make your own programs using Scratch and Python. But let us first have a look at how to run programs in Scrath and in Python on the Raspberry Pi. Running Programs with Scratch ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Running programs on Scratch is very easy, so is building them! To start, launch Scratch GPIO from the Desktop... Once Scratch opens, you can drag and drop elements from the left side to the middle, and then run your program inside Scratch. You can see an example below which makes Scratchy the cat break-dance forever: .. image:: img/scratch_example.gif :align: center Writing Python Scripts ^^^^^^^^^^^^^^^^^^^^^^^ .. _pythonShell: The Python Shell ---------------- From the programs menu on the RPi, open IDLE (Integrated Development and Learning Environment). .. image:: img/python_launch.jpg :align: center The Python Shell will open to welcome you! The shell allows you to interact directly with Python, which means that the instructions you type in will directly be executed. Let us try a few examples: .. image:: img/pythonShell.gif :align: center How to Write and Run a Python Script? ------------------------------------- Once the Python shell is open, click on “File” and in the drop down menu click on “New Window”. A new window will appear. This is where you will write or paste your Python program. The file has to be saved before it can be run. Try it with a simple example: .. image:: img/pythonScript.gif :align: center The program can be run from Python directly by pressing F5 or from the “RUN” command in the file menu. Another way to run a program is by using the Terminal. This can be done by typing the following command followed by Enter: .. code-block:: shell sudo python file_name.py where `file_name.py` is the name of the file that was saved. Here is a screenshot showing how to execute a script called `simple_test.py`: .. image:: img/python_terminal.png :align: center .. note:: The command: sudo python file_name.py could also be written as: python file_name.py which in english means to run the script inside file_name.py using python. However, there's one major difference between them: the second way does not give Python the same power the first one does. **sudo** which stands for: "Super User DO" lets Python have more access to the Raspbian system and to the Raspberry Pi as a whole. This will be very useful later, when we have some code that needs to control data and electronics which are only available for a super user. For better or for worse you can always write sudo to guarantee your code will run successfully: sudo python file_name.py How to Navigate in the Terminal? -------------------------------- All we need to know for now is listed bellow: +-------------------+--------------------------------+ |**pwd** | Print working directory | +-------------------+--------------------------------+ |**cd** | Change directory | +-------------------+--------------------------------+ |**cd ..** | Change to parent directory | +-------------------+--------------------------------+ |**ls** | List files in current directory| +-------------------+--------------------------------+ Try out these commands like in the example! .. image:: img/terminal.gif :align: center .. tip:: Be lazy! - Use the **Tab key** to use auto-complete. - Use the **Up Arrow** to re-call previously typed in commands. | .. seealso:: You will find a few basic linux commands here: http://www.pas.rochester.edu/~pavone/particle-www/telescopes/ComputerCommands.htm