Category Archives: Raspberry Pi

Drive a 16-Channel Servo with the Raspberry Pi

To drive servomotors micro-controllers must have PWM outputs. These are output pins on which the micro-controller will generate pulse outputs with controlled or modulated variable widths. Most embedded micro-controller units have one or more of these outputs. The famous single board computer, the tiny credit card sized Raspberry Pi or RBPi also has one IO pin dedicated for PWM. This is the PWM channel available at the GPIO18 of the RBPi and with this, you can drive a single servo at best. However, if you want the RBPi to drive more than one servo, it will need additional circuitry.

A PWM driver IC such as the PCA9685 can drive 16 servos at a time, but requires commands and data through its I2C interface. Fortunately, the RBPi can also communicate using the I2C protocol, enabling it to control 16 servos via the PCA9685. Adafruit has a very convenient breakout board with the PCA9685 on it and that makes it very convenient to connect to the RBPi. Not only can you drive servos with the PWM outputs, you can use the PWMs for controlling LED lighting as well.

To let RBPi communicate with the I2C protocol, it will require a special OS available from Adafruit. This is the Occidentalis flavor and it has all the libraries required for invoking I2C. However, if you are using the stock Raspbian OS, you must install the python-smbus and the i2c-tools using the “sudo apt-get install” command. To learn more about using I2C, refer Adafruit’s rather informative tutorial.

The two packages will allow you to search for any I2C device connected to the RBPi. The easiest way you can connect the servo breakout board to your RBPi is with the help of the Adafruit Pi Cobbler. Here, VCC is the digital supply for the IC or 3.3V, and V+ is the supply for the servomotors (typically 5V).

The actual chip that drives the servos, the PCA9685, needs 3.3V, and connects to the VCC on the cobbler board. Servos usually require much higher currents to operate. Therefore, they are powered from a separate power supply, typically 5V, and are connected to the V+ on the Cobbler. Note that this 5V is different from the 5V supply for the RBPi. The PWM operation on the servos creates a huge amount of electrical noise, which can cause the 5V supply voltage to fluctuate significantly. RBPi may not be able to tolerate such voltage fluctuations, and this may cause it to crash and lock up.

If you are driving many servos, it will be a good idea to add a capacitor to the driver board. There is a spot already marked for such a capacitor. As a thumb rule, you need a capacitor with a value n x 100uF, where n is the number of servos you are driving. Capacitors are manufactured in standard ratings, and you may have to go for the next higher standard value that you have calculated.

Depending on whether you are using a standard or continuous rotation servo, your python code will vary. For the actual code with which you can control the various parameters of I2C and hence the servo, you may refer to this site.

Is the Odroid SBC Better than Raspberry Pi 3?

The world of inexpensive SBCs or single board computers has been taken by a storm with the unveiling of the new Raspberry Pi board or the RBPi 3. The claim being it blows the competition away, and that no one can match its price. However, that may not be entirely true, as the Odroid C2 SBC seems to best the RBPi 3.

Hardkernel promotes its Odroid C2 as another cheap and speedy SBC with a 64-bit ARM-based quad core processor. A comparison of the specifications shows the C2 may be giving the RBPi 3 a run for its money. Compare for instance, the BCM2837 of the RBPi 3 with the Amlogic S905 SoC of the C2. Although both are quad-core ARM Cortex-A53, the C2’s processor runs at 2GHz to the 1.2GHz of the RBPi 3. At 2GB, the C2 has double the RAM of the RBPi 3, which has only 1GB. Moreover, the C2 comes with a Mali-450 GPU, able to deliver 4K video.

Although the C2 does not have the on-board wireless and Bluetooth features of the RBPi 3, it has a high-speed Gigabit Ethernet port directly wired into the SoC. The RBPi 3 also has Ethernet on-board, but as this is a 100-megabit port and is on a USB interface, its speed is likely to be limited.
The two boards share a very similar form factor and are nearly identical in their GPIO capabilities. In addition, for both the boards, you can choose the storage to be either the usual micro SD card or eMMC. However, it is worth stating that the C2 comes with a built-in ADC or analog to digital converter. When it comes to operating systems, the C2 can operate with Ubuntu 16.04, or Android lollipop.

The RBPi family, just like Apple products, has always faced competition. However, most look good only on paper, but their prices always let them down in the end, and we never hear of them after some time. The price of $40 for the C2, being very close to that of the RBPi 3, may just escape this fate. Of course, there is the matter of adding shipping and customs to the price, as the origin of C2 is Korea.

So, which of them is preferable – the C2, or the RBPi 3 – and why? The faster processor of the C2 and its faster wired networking would make it attractive to someone working on network-attached data processing applications. Although one can add a USB wireless network adapter for only a few dollars, the onboard Wi-Fi and Bluetooth of the RBPi 3 makes it so much more attractive. Therefore, the RBPi 3 would be coveted by anyone who is a home user or planning to use a computer on a platform that will remain unfettered by wires.

Although the C2 may be more impressive when compared to the RBPi 3, the latter will likely outsell the C2 many times over. This may not be because of the massive publicity advantage that the RBPi 3 is receiving from the Pi foundation, but more likely due to the wide ecosystem of hardware and software developers the RBPi family has at present.

The ExaGear Desktop for the Raspberry Pi

Normally, the Raspberry Pi or RBPi does not allow running Intel x86 applications. This is because the RBPi is ARM-based. That means it has a different architecture from the Intel-based PCs we are used to using. This is as if a letter addressed to a Russian town landing up in Denmark – the address is all wrong, so it is tough to deliver.
Virtual machines are available that create a local environment for running applications where the basic architecture differs. For the x86 platform, the most popular virtual machine software are VMware and VirtualBox. With virtual machines, you may be running Linux as your main operating system, but you can also run a full-fledged Windows operating system simultaneously and vice versa. The main operating system is termed the Host, while the OS running under the virtual machine is termed the Guest.
Eltech has produced such a virtual machine for RBPi that have ARM platforms as their base. This is the ExaGear Desktop and it allows you to run Intel x86 applications directly on your RBPi through a virtual x86 Linux container on ARM. For example, on the ExaGear Desktop, if you install Wine, the open source compatibility layer software application will allow you to run even Windows applications on your RBPi.
You can run the ExaGear Desktop on most ARM-based Mini PCs operating with Linux such as the RBPi, Banana Pi, Wandboard, Jetson TK1, Utilite, CuBox, CubieBoard, ODROID including the ARM-based Chromebook. Unlike Linux, ExaGear Desktop is not free and you can download it only after paying for its license key. However, before buying, it is prudent to check up if your Mini PC has the proper hardware and software base to allow ExaGear Desktop to run on it.
If you are using the RBPi ver1, you will need the ARMv6 instruction set with VFP32. For other RBPi versions and ARM devices, you will require the ARMv7 instruction set with VFP32. If you are planning to use x86 applications that require MMX/SSE, you will also need NEON as support. On the software side, you must be using the Linux operating system variants such as Raspbian, Debian 7, Ubuntu 14.04 or Ubuntu 12.04. Check with the Eltechs Tech Forum if you still harbor doubts about system requirements.
Eltech has published some test results to demonstrate the speed with which the ExaGear Desktop works. For benchmarking, they have used SysBench, which was built for ARM and Intel x86 platforms. Using the same ARM machine for both tests, they have compared the results of ARM-based tests against x86 tests running under the ExaGear Desktop. The tests cover parameters such as File IO read/write, CPU cycles, Memory usage, Threads speed and Mutex. Results show ExaGear to be superior to QEMU in almost all parameters.
Using their setup, Eltech has also compared the performance of ExaGear against the performance of QEMU, the user mode emulator. For benchmarking, they used GeoBenchmark and found that ExaGear Desktop was nearly five times as fast as QEMU was.
Eltech has also compared the ExaGear Desktop performance against QEMU using the nbench benchmark. Here too, ExaGear Desktop was able to show far superior performance compared to the performance of QEMU when both were run on the same platform.

Stackable Pi-Plates for the Raspberry Pi

If you are faced with a paucity of projects for your Raspberry Pi or RBPi, the tiny, credit card sized single board computer, you should get the circuit boards from Pi-Plates and connect your RBPi to the outside world. Pi-Plates offer a family of stackable, add-on boards that provide your SBC with a robust set of features at a minimal cost.

Pi-Plates design their circuit boards to be economical with the GPIO pins they use from the RBPi header. For example, when using the DAQCplate board, it uses only two dedicated GPIO pins. However, you can stack eight of these Pi-Plates to get 64 digital inputs, 56 open-collector outputs, 64 analog inputs and 16 analog outputs. Whether you are an experimenter, a hobbyist or a professional, Pi-Plates have designed these boards to be useful for all. Additionally, these are mechanically and electrically compatible with all revisions of the RBPi. That includes versions A, B, A+, B+ and the new version 2.

At present, Pi-Plates offer four products. The flagship product is the DAQCplate board that has ADCs or Analog to Digital Converters, DACs or Digital to Analog Converters and expanded digital IO. MOTORplate is a new product for controlling motors and you can use it to drive two stepper motors or four DC motors, while its onboard software can handle all drive logic including acceleration profiles. If you want to add custom hardware on your Pi-Plate stack, you can use the PROTOplate board.

When stacking Pi-Plates, you will need a secure structure and this is provided by the BASEplate mounting system. All hardware necessary for mounting to the BASEplate is already available with each Pi-Plate board. Pi-Plate also offers two great kits.

The DAQC kit comprises two BASEplates and one DAQCplate boards for the price of a single unit. This makes a great beginning for those starting with the DAQCplate for the first time.

For those starting with a MOTORCplate, the MOTOR Kit may be very useful. This kit comprises one MOTORplate and two BASEplate boards for the price of a single unit.

For example, the DAQCplate is a data acquisition and control board. Its digital output section has a connector that provides seven open-collector outputs and a pair of 5VDC outputs that you can use for driving loads. You can protect these with a flyback diode connected to the terminals.

You can use these outputs to drive incandescent automotive light bulbs, ultrasonic rangefinders, resistive heating elements, unipolar stepper motors, buzzers, solenoids, relays, DC motors or LED strings. Green LEDs connected to each digital output light up to indicate a high on the output. To light up these LEDs, you do not require connecting anything to these outputs. At the same time, these LEDs will not affect anything that you connect to these outputs.

Darlington pair transistors drive the seven open-collector digital outputs. They can sink a maximum of 350mA and handle a maximum load voltage of 12VDC. With a load voltage of 200mA, the on voltage is typically 1.1V. When using inductive loads such as solenoids or relays, you must connect the high side power supply to the flyback protection terminal.

Playing 4-Bot with the Raspberry Pi

Sometime or the other we have all played Connect-4 or Four-in-a-row against either a human or a computer opponent. It is a simple game where you and your opponent each try to get four same-color pieces in a row, while trying to prevent the other from doing so. The first one to line up four adjacent pieces of the same color wins the game.

Conventionally, the game board has 42 squares made of six rows and seven columns. Players start with several discs of two colors each, and to be successful, each player has to constantly plan and revise their strategy. Therefore, an SBC or single board computer such as the Raspberry Pi, or RBPi is a suitable candidate for playing Connect-4. Besides enjoying the game, you hone your skills as a DIY enthusiast by building the game. Of course, this project will require some skill in mechanical assembly, and in coding as well.

You can have a horizontal board and an X, Y arm mechanism to let the RBPi deliver its pieces to the required square. However, a vertical board makes the mechanism simpler, as the arm then has to travel only in one axis, gravity taking care of the other. The vertical board is actually made of two faces, with a gap in between and separators to mark the columns to allow the discs to be dropped in one of the columnar spaces between the two faces. Both faces have 42 matching circular cutouts, so it is easy to see where each disc is positioned. A claw on the arm mechanism picks up a disc from a stack, positions itself above the required column, and releases the disc, allowing it to fall in the column between the board faces.

The software requires the use of Python Imaging Library for processing the image of the game board. To enhance readability, the image can be down-sampled to 16 colors, and then divided into a grid. It is only required to identify each of the 42 spaces on the board as red, yellow, or empty. This is easily done by reading the RGB value of each space in the grid, and saving this data in the form of an array. This forms the board state after every move and this is passed on to the AI or Artificial Intelligence on the RBPi for calculating the next move.

The AI used is a well-known algorithm known as Minimax – applicable to games of this nature, and there is a Python library for Minimax. Using tree-searching methods, the algorithm looks several steps ahead to calculate the next best move. Getting the RBPi to play effectively can be quite a challenge, as even a small Connect-4 board of 6×7 squares can have 4,531,985,219,092 possible game positions. Therefore, the program tries to trade-off between absolute perfect play and reasonable time for each move. If you can strike a balance between the two, the RBPi can play quite intelligently, but still complete each move in about 25 seconds – this is acceptable for a flowing game.

CORATAM with the Raspberry Pi

The ubiquitous Single Board Computer, the Raspberry Pi, or the RBPi is a perfectly suitable candidate for CORATAM or Control of Aquatic Drones for Maritime Tasks. Sitting within each drone, an RBPi becomes a part of a swarm of robotic systems. Portugal is using this novel method for exploring and exploiting its maritime opportunities as the sea is one of the country’s main resources. Although land-based and air-based swarms of robots have been extensively used for studying the aquatic environment for the proposed expansion of Portugal’s continental shelf, swarms in aquatic environments are a different breed altogether.

Tasks in aquatic environment are usually expensive to conduct. This is because of all the special operational requirements of manned vehicles and support crews. Therefore, Portugal has thought of an alternative approach where they have used collectives of relatively simple and inexpensive aquatic robot swarms. As each robot is easily replaceable, these have a high potential of applicability for essential tasks such as prospecting sites for sea border patrolling, bridges inspection, sea life localization, environmental monitoring, aquaculture, and so on.

The collectives of robots work on a decentralized control based on the principles of self-organization. This gives them the capability of performing efficiently on tasks that require robustness to faults, scalability, and distributed sensing.

With the development of CORATAM, Portugal is hoping to achieve three main objectives. The first is to explore the novel approach of control synthesis in a set of maritime tasks, but in the real world. The second is to develop a swarm of aquatic robots with fault-tolerant ad-hoc network architecture, heterogeneous in nature and scalable. The third is to disclose all the hardware and software components developed under an open-source license, to enable others to build their own aquatic robots.

Each robot is about 60 cm in length, and inexpensive, as the designers have used all widely available, off-the-shelf hardware. Each robot uses a differential drive mono-hull boat, which can travel at a maximum speed of 1.7 m/s, in a straight line. The maximum angular speed the robots can achieve is 90°/s.

An RBPi-2 SBC supports the on-board control of each robot. They communicate via a wireless protocol (802.11g Wi-Fi) and each broadcasts its UDP datagram. The neighboring robots and the monitoring station receive the broadcast, forming a distributed network without any central coordination or a single point of failure. All robots are equipped with compass sensors and GPS, and each broadcasts its position to the neighboring robots every second.

All robots use prototype hardware, making it inexpensive when compared to the majority of the commercially available unmanned surface vehicles. Therefore, the robots serve as a platform suitable for research and development, and easily maintainable. Additionally, the open source nature of the platforms makes them suitable for different manufacturing processes, sensory payloads, design choices, and different actuators to be used.

An artificial neural network-based controller controls each robot. The normalized readings of the sensors form the inputs of the neural network, while the output of the network controls the actuators on the robots. Each sensor reading and actuation value is updated every 100 ms.

CHIP Competes With the Raspberry Pi

The extremely popular tiny, credit card sized, inexpensive, single board computer, the Raspberry Pi or the RBPi may soon have a rival. So far, the contender, known as the CHIP, is waiting for its crowdfunding project to complete. In the future, expect more of such similar devices jostling the market place.

Unlike the RBPi, CHIP is completely open source – for both its software and its hardware. Once in the market, the design and documentation will be available to people to download. Therefore, with the schematic available, people will be free to make their own version and add improvements or tweaks to the design.

CHIP’s operating system is based on Debian Gnu Linux, which means it will support several thousand apps right out of the box. On the hardware side, there are some improvements on the specifications of the RBPi. As against the 700MHz CPU of the RBPi, CHIP runs on a single core CPU at 1GHz. Users can do without the SD Card, as CHIP has storage memory of 4GB built into the card. The 512MB RAM is the same as that in the later models of the RBPi. While users have to add separate dongles for Wi-Fi and Bluetooth when using the RBPi, CHIP has both built on-board.

CHIP can connect to almost any type of screen. Its base unit offers composite video output, but there are adapters for both VGA and HDMI. An optional case for the CHIP enables it work with a touchscreen and a keyboard. The entire package is the size of an original Game Boy.

All this may not be surprising since there have been prior competitors with better specifications and more features than those of the original RBPi do. However, all the competitors so far were unable to beat the price factor – they were all more expensive than the RBPi. This is the first challenger bringing the price lower than that of an RBPi – the basic unit of the CHIP costs only $9. The Next Thing Co., the manufacturers, call this the “world’s first nine dollar computer,” and in their opinion, CHIP is “built for work, play and everything in between.”

Along with a lower price tag, CHIP has a smaller profile than the RBPi. As it has a more powerful processor and more memory, CHIP could easily replace RBPi as the primary choice for projects. The entire board is packed with several sockets and pins. Its hardware features include a UART, USB, SPI, TWI (I2C), MIPI-CSI, Eight digital GPIOs, parallel LCD output, one PWM pin, composite video out, mono audio in, stereo audio out and a touch panel input.

Users of CHIP will learn coding basics and play games on the tiny computer that may soon usurp the title of king of the budget microcomputers, so far being enjoyed by the RBPi. CHIP measures only 1.5×2.3 inches and is compatible with peripherals such as televisions and keyboards. It runs on Linux, works with any type of screen and comes with a host of pre-installed applications. Therefore, users can simply make it work out of the box, without having to download anything.

Raspberry Pi and a Simple Robot

Using a pair of DC motors and connecting them to two wheels can be the basics of a simple robot. Once you add a single board computer to this basis structure, you can do almost whatever your like with your robot. However, making a robot do more than simply run around requires many mechanical appendages that may prove difficult to get unless you have access to a workshop or you are proficient with 3D printing.

To simplify things for beginners, the robot chassis from Adafruit is a versatile kit. With this simple robot kit and a single board computer such as the Raspberry Pi or RBPi, you can start your first lessons in robotics.

As the kit is for beginners just starting with their first robot, there are no sensors. A Motor HAT (Hardware Attached on Top) controls two motors connected to two wheels on a chassis. The front of the chassis has a swivel castor, which makes it stable. The RBPi mounts on the chassis and a battery supplies the necessary power for the SBC and the motors.

Once you are familiar with generating a set of instructions in Python to make the robot move the way you want it to, you can start adding sensors to the kit. For example, simply adding a camera will allow the robot to see where it is going. Adding an ultrasonic range finder will allow the robot to avoid bumping into obstacles in its path.

The Mini Rover Robot Chassis Kit from Adafruit includes almost everything one needs to build a functional robot. It has an anodized aluminum chassis, two mini DC motors, two motor wheels, a front castor wheel, and a top plate with standoffs for mounting the electronics.

It is convenient to use the latest RBPi models such as the Model 2, B+, or A+, as these have suitable mounting holes that allow easy attachment to the robot chassis. Although it is also possible to use the RBPi Zero, its small size makes it unsuitable to mount the motor HAT securely.

The Motor HAT can drive DC and stepper motors from the RBPi and is suitable for small robot projects. The brass standoffs help to hold the Motor HAT securely to the RBPi. Power comes from two sources. One 4x AA battery pack supplies the motors. Another small USB battery pack powers the RBPi. The RBPi also requires a Wi-Fi dongle to keep it connected to the computer and to control the RBPi robot.

Your RBPi must be running the latest version of the Operating System – Raspbian Jessie. If you do not have this, allow the RBPi to access the Internet and download the necessary software.

The Motor HAT library examples included provide adequate software for this project to start. For example, you can use the example scripts provided to make the robot move forward, backward or to turn in different directions. Preferably, place the robot on level ground, where there are no obstacles. As the robot has no sensors, it can hit something or easily fall off the edge of a table.

Remote Controlled Car with a Raspberry Pi

A single board computer such as the Raspberry Pi or RBPi can work wonders on a remote controlled car. Running Python on the RBPi allows it to handle three tasks a remote controlled car needs most – self-driving on a track, detection of sign and traffic lights and avoiding front collisions. The RC car has three subsystems – input units consisting of a camera and ultrasonic sensors, a processing unit and a control unit.

The processing unit on the RC car communicates with the RBPi to handle several tasks. These include receiving data from the RBPi, training, and predicting the neural network, detecting objects, measuring distances, and sending instructions to the Arduino through the USB connection.

The computer also runs a multithread TCP server program for receiving streamed image frames and ultrasonic data from the RBPi. The computer converts the image frames into gray scale and decodes them into numpy arrays.

To make object recognition and steering simple and fast, the RC car uses a neural network. The advantage is once the network is trained, it can work with only the trained parameters, making predictions very fast. The output layer of the network has four nodes corresponding to the steering control instructions – forward, reverse, left, and right. The input layer has over 38,000 nodes and uses only the lower half of the input images for training and prediction.

Although the project uses the shape-based approach for object detection, it only focuses on detecting the stop sign and traffic lights. Detection and training was both using OpenCV using both positive and negative samples. Positive samples are images that contain the desired object while negative samples are random images without the desired object.

The controller on the RC car needs four low-going signals corresponding to the forward, reverse, left, and right actions. Four pins on the Arduino provide these signals simulating button-press actions that drive the RC car.

The ultrasonic sensor measures the distance of an obstacle in front of the RC car. This includes measuring proper sensing angle and other surface conditions. Other measurements from the Pi camera allow the RC car to stop at the correct distance from the object.

The monocular vision approach of the RC car makes it difficult to get accurate distance measurements. In turn, other factors also influence the distance measurement, which includes errors in the actual measurement, variations in detecting the bounding box of the object, and nonlinear relationship between distance and camera coordinates. The error increases when camera distances are great and the camera coordinates are changing rapidly.

The traffic light recognition process uses image processing for detecting red and green lights. First part of the training involves detecting the traffic light by decoding its bounding box. Next, Gaussian blur reduces the image noise to find the brightest point within the bounding box. Finally, red or green state determination within the brightest spot detects the actual state of the traffic light.

The project uses an RBPi Model B+, a Pi camera and an ultrasonic sensor, HC-SR04. The RBPi streams ultrasonic sensor and color video data via its local Wi-Fi connection. It scales the video down to QVGA resolution to achieve low latency.

A Microscope with the Raspberry Pi

If you require a microscope, you can make one as a proof-of-concept using the RBPi or Raspberry Pi. It is simpler if you have a bagful of LEGO parts to build the structure, but you can also go with Plexiglas construction. Apart from being a useful addition to a science laboratory, making a microscope with the RBPi is a good way of learning computer programming and making things with your hands.

The microscope uses an electronic camera for resolving images and its maximum resolution is about 5µm per pixel. That means you will be able to see and analyze dust, salt, hair and fruit flies – objects mainly in the range of a 20th of a millimeter to 5mm. Since at high resolutions only a small area will be in focus, you may confront distortion and color effects, commonly known as chromatic aberration. That precludes seeing cell culture or blood cells.

If you make the microscope construction from pre-produced parts and do not glue them together, it will allow for subsequent modifications, optimizations and adaptations for special applications, if necessary. You will need an RBPi2 with its SD card, a keyboard, mouse, a monitor or TV. You will also need an electronic camera similar to the WaveShare B, along with a 50 cm cable. For the pre-produced parts, you can refer here. The illumination comes from a 1.6W LED lamp working off a 9V block battery, operated through a small switch.

The construction of the microscope starts with a base plate and a sled tray for placing and holding objects or object glasses. Then there is a tower for holding the plate, which acts as the camera mount. You should be able to move the camera plate and the object sled orthogonal to each other for placing the camera precisely above the object.

There are two ways to focus the camera. You can adjust the length of the columns of the camera tower to get a coarse adjustment – this will adjust the distance between the object and the camera lens. For a better focus, you can then turn the camera objective manually. You may have a worm gear arrangement with a toothed rack (possibly from the LEGO collection) and you can use that to adjust the focus. The gear wheel with toothed rack could guide the object tray and the worm gear could be attached to the camera.

For processing images from the camera, there is a large choice of software to use. You can use very good GUIs available for raspivid (video capture) and raspistill (for still images). Alternatively, you can use raspistill along with Mathematica and its image analysis functions, for processing the images for subsequent analysis.

You can also use PiVision, which offers an option to preview the image to see if the camera is properly focused on the area of interest, before capturing the image as a still photo. During preview, PiVision allows changing the options setting for expanding the preview image to get more details and to re-focus, if necessary. Once you have captured the image, remove the unwanted areas by cropping it.