Learn more about Python's traits and history as a popular programming language and how it can be used in maker projects.
In this article, weâll be learning about one of the most popularly-used, programming languages today, Python. Weâll examine its history as a language, itâs functions, as well as the various ways it benefits makers.
The Birth of Python
The Python language was established by an engineer, Guido van Rossum in the late 1980s. By creating Python, the founder was intending to improve upon the general-purpose ABC programming language (such as including features like exception handling).
The Python language has several axioms that direct how the language should work and be written.
- It should be organized and easy to understand.
- It should be a simplistic rather than complex language
- It should be readable.
- Its rules should be conveyed as clearly as possible.
Python is only one of many computer programming languages available to makers and engineers alike. However, there are many alternatives, including C, C++, Java, and even assembler. To understand the benefits of using Python over other languages, itâs important to recognize itâs specific qualities when compared to them.
Quick to Prototype
When it comes to prototyping, Python is by far, one of the fastest languages to codeâhere are a couple of reasons why.
Python (when executed on a PC), has access to a PIP installer which makes the installation of libraries extremely convenient by performing a simple input.
In other languages such as C and C++, the process of installing libraries can be complex and may require editing the compilation command.
Python is a very high-level language with many complex procedures preprogrammed into itâwhich translates into less code needed to complete the same task written in C or C++.
Even though the Python language can be considered simpler to use, it still offers a wide range of features found in most other languages such as multitasking, multiprocessing, and classes (object orientation).
However, Python is a scripted language as opposed to being compiled, which means that the computer reads the Python code line by line, interprets what each line means, and then performs the action.
This can be a lengthy process that involves the computer opening file streams and interpreting instructions as opposed to actually executing commands.
Which results in Python programs typically running slower than those written in C and C++.
Another disadvantage of Python is that it cannot be used on most microcontroller platforms (on account of it being scripted, not compiled), such as the Arduino, PIC, and AVR series of microcontrollers.
There is a version of Python that fits onto a microcontroller called âMicroPythonâ but this is a stripped down version and requires a moderately powerful microcontroller (32-bit ARM for example).
Devices Compatible with Python
When it comes to maker needs, makers, itâs beneficial to know the specific hardware that the Python language is capable of interacting with; rather than itâs use in recording data or producing simulations. We break down the most notable applications below.
Raspberry Pi is a popular, single-board computer thatâs powered by an ARM core processor.
It can be programmed using Python and provides easy access to GPIO. This allows for Python and Raspberry Pi projects to interact with hardware such as LEDs, motors, and switches directly.
If an application requires a powerful microcontroller with I/O access and a sizable RAM capacity; this pairing will easily provide those specifications. Especially when utilizing the latest Wi-Fi integrated, Raspberry Pi, B model computers.
The serial port is one of the most useful hardware connections that a hobbyist can use.
Serial ports allow a device to communicate to a PCâwhich could involve the transfer of data from a sensor or controlling a motor.
Serial ports are incredibly easy to interface with and only the port name is needed (such as COM3). Almost any hobby project can be easily connected to a computer USB port when utilizing USB-serial bridges.
Both the device and the serial port need to be operating at the same baud rate, but the port on a computer has an adjustable baud rate which makes it easier to connect to a range of devices.
The PySerial module provides a serial port library for Python and is considered essential to any project using the language.
Machine vision is another popular add-on for Python and allows a program to record information from a webcam with the capability of face-recognition and outline objecting specified objects.
A specific library that helps record information and a large number of image processing tasks is OpenCV (Open Computer Vision).
Python and OpenCV can be coupled to enable facial recognition for security authentication, object tracking for turrets, object avoidance, feature detection, and even 3D reconstruction.
Internet and IoT
By default, Python has several built-in, network libraries that allow it to create sockets and communicate online using TCP (Transmission Control Protocol) standard.
Popular protocols such as MQTT and Adafruit.IO can be used in projects to provide internet capabilities, including sending emails, communicating with IoT servers, and sending messages to other Python programs.
IoT projects are one of the more popular uses for internet connectivity in maker projects, commonly when handling data such as sensor readings or motor states and sending them to a remote server, where data can be viewed on a graphical scale.
Another common use involves manipulating environmental controls. Like temperature and humidity readings being sent to a server, which will then relay the data to a heater, that can automatically turn on when the temperature is low.
Due to Pythonâs capability of running on platforms such as Raspberry Pi, there's no doubt it can be utilized in many projects.
You'll find a few examples below of what makers have done with Python.
This home automation project allows an IoT-enabled device to control other typical devices found at home using a Raspberry Pi.
With the help of IBM Bluemix, commands from a website are sent to the IBM IoTF and Bluemix framework which can then relay messages to a connected internet enabled device such as a Raspberry Pi.
The Raspberry Pi can also read data from a PIR sensor and relay this information back to the cloud so intruders can be detected remotely.
The completed home automation project using a Raspberry Pi, Python, and the IBM Bluemix.
With the help of the telegram bot library for Python, any computer running the language program can be turned into an intelligent telegram bot.
In this project, a Raspberry Pi was connected to an LDR, which was able to determine whether it's sunny or overcast.
If it determines that the sun is out, then a Python-coded message is sent by Telegram, the cloud-based, instant messaging application to all connected users.
While this specific example uses a Raspberry Pi, the code can also be executed on a PC through the serial port to read data from an LDR connected to an Arduino Uno.
Python Powered Cat Toy
Like the sun informer, this project takes advantage of a Python and Raspberry Pi pairing to create a smart cat toy.
The idea behind this project is to have a Raspberry Pi read the analog voltage across a photoresistor; which is then used to trigger a spinning disk.
This experiment demonstrates the simplicity of connecting a computer to I/O pins, which would otherwise be complex in other programming languages such as C and C++.
For more information or project examples on Python-related topics, check out some of our other articles below!