RandA: Merging Raspberry Pi and Arduino

By on February 12, 2015

RandA45

Today we present a Bridge between Raspberry Pi and Arduino that leverages the hardware equipment and the computing power of the first with the shields and the applications of the second.
The worlds of Raspberry and Arduino increasingly tend to converge to give users a bridge between the enormous potential of the embedded Linux platform and the amount of shield and sketches for the Arduino world. Raspberry and Arduino are now the absolute stars in the firmament of hobbyists, digital and not. Raspberry is a complete system with a professional multitasking Linux operating system; while Arduino is mono-task it has a natural predisposition to the input / output and, especially, has a practical development system. The Arduino IDE (Integrated Environment Development), runs on any PC and allows you to create programs (called sketch) and download them automatically on the processor.
Arduino can also count on a large number of add-on cards, developed by the Arduino organization and by third parties, to meet any interfacing need to the outside world, both input and output. The hardware “de facto” standardization and expansion connectors has allowed the proliferation of these additional cards (called shield).
The best way to have the two worlds collaborating is to exploit specific characteristics of both. For example:

a) Raspberry Pi could use Arduino as configurable device; its code/application is chosen from time to time, based on a library of available sketches, and is automatically downloaded to Arduino

b) on the contrary, Arduino might work as a controller for Raspberry Pi, i.e. it could launch command and dialogue with it; in practice, Raspberry become a sophisticated Arduino extension, allowing access to complex environments like the network, or allowing complex processing or access to multimedia.

1106_Schema

Hardware connection can be easily implemented by using the USB connection, or via a link through the serial port, if the hardware is fully integrated. And this is what we have achieved: a card for Raspberry with Atmel processor 328 without USB but with all the remaining hardware like Arduino Uno, including of course the connectors for mounting shield. The dialogue between Raspberry and Arduino takes place via the serial port that both possess.
This card, which we named RandA (Raspberry And Arduino), is ready for the new Raspberry plus, but is compatible with the old Raspberry.
Because of Raspberry feels the lack of an RTC (Real Time Clock) and a programmable power on/off button and sleep timer, we decided to put them on the board. Even on Raspberry Plus, powering occurs by connecting the power supply through the micro USB connector or via an upstream circuit breaker provided by the user. As for turning off, you must run the command “shutdown” and then disconnect the power.
To solve this cumbersome we inserted on the card two additional 5 Volt power connectors, an additional micro USB and a screw terminal. These connectors feed Raspberry and Arduino via a circuit controlled by a button. In addition, a software installed on Raspberry detects the button action and launches the shutdown and subsequent turns off power. The button, then, controls the power on and shutdown of Raspberry. A two-pin connector can replicate the button outside any case.

Fig2

However, there is more: you can turn on Raspberry Pi and Arduino at scheduled times. In fact, we have put an RTC with programmable alarm clock. This RTC, in addition to providing Raspberry a clock even when not connected to the Internet, is also connected to the power circuit. The clock is operated by Raspberry via I2C connection and for its operations we have provided some Linux commands, but also the ability to manage it via the web, as we shall see in the next in-depth articles.

Fig3

Finally Arduino can turn Raspberry on. In fact, by a switch, it is possible to disable the on / off switch for Arduino, which is so always powered. Since RandA contains a basic Arduino, without the voltage regulator circuit and without USB, its consumption can be very low. So low that you can think to feed it with batteries and solar panels. At this point, a link between Arduino and the power button is still missing. We have achieved this, through an output pin.
This way Arduino can work as Raspberry Pi controller and turn it on in case there is some condition detected by its digital or analog inputs. After switching on, Arduino can pass the ball to Raspberry Pi and run its commands, or it can continue to manage the event on its own initiative by activating programs on Raspberry Pi.

 

How Raspberry and Arduino communicate

As mentioned, the basic connection between the two worlds is via the serial line, since both Arduino and Raspberry are equipped with. The physical link is made via the Raspberry connector, where RandA is connected (but only on the first 20 pins).
The serial port is used for both Arduino programming and for communications between it and Raspberry. In addition, through a GPIO of Raspberry, is managed the Arduino reset.
A Linux interface script that activates the reset in an appropriate way does the Arduino programming. In fact, in standard Arduino, the uploading of the sketch is preceded by a reset signal triggered automatically by the circuit that manages the USB, which is missing here.
The Arduino programming can take place locally via the IDE installed on Raspberry and adapted by us. Or remotely using an IDE for PC, modified to handle an Arduino connected to the network by simulating a remote port.

Fig4

Finally, as we shall see in future articles, you can upload sketches (compiled) to Arduino via WEB interface.
If Raspberry Pi wants to use Arduino, it can send data or directives through programs that use the serial port of Rasperry Pi and of which there are examples prepared, in both C and Java.
If Arduino is commanding “dances”, then you need that on Raspberry Pi there is a process in “background” ready to detect the arrival of commands. To achieve this, we were forced to use an additional Raspberry Pi GPIO and an Arduino pin to trigger this process. This, because of the single serial communication line. In any case, we have provided a library for Arduino to:
✦ sending Linux commands and receiving a reply, if any;
✦ opening a file on Raspberry Pi and the reading / writing on it.

We also prepared Linux commands (scripts and programs) to manage the clock, alarm and sending email.

How to develop programs

As we have seen, Arduino can be programmed either through an IDE installed on your computer, or with a local IDE (Linux).

But how to use the IDE on Raspberry Pi, and in general how to access Linux environment in a practical way?
Sure, Raspberry can be managed by connecting the keyboard, mouse and monitor to USB and HDMI connector. But the usual use of Raspberry is through Network; via the Ethernet port or via a USB WiFi dongle.
In this case the access is with SSH protocol, automatically enabled in the latest Raspbian operating system distributions. But instead of using the program PUTTY, installed on all computers and well known, we suggest that the software “MobaXterm”, more powerful and equally free (Fig.. This software allows you to create an SSH connection as PUTTY, but also has a xserver automatically managed.)

fig5

This means that the graphical interfaces of Raspbian are properly displayed on the remote computer. Or you can manage Raspberry Pi as if they were connected keyboard, mouse and video. Although you can run the full desktop, it is preferable for performance reasons, use the interface to the console as a base command and activate graphical programs when needed.
For example you can run:
✦ command file management (like Windows Explorer);
✦ graphic editor;
✦ development environments: for C (as CodeBlocks), for Java, or the Arduino IDE.

Xwindows windows appear on the remote computer automatically thanks to the built-in MobaXterm Xserver. Actually, MobaXterm also allows you to open an FTP session and more.

Fig7

Now all that remains is to decide which program to develop and use the appropriate environment: script editor for Linux, C IDE, Java, Python etc.. for programs and the Arduino IDE, local or remote, for Arduino.
In any case, we will distribute software to install, which will include both programs for operating RandA, development environments, the libraries for Raspberry GPIO management and several examples.
Finally, we are preparing a Web Server (Tomcat) that allow remote management of RandA via Browser.
In particular it can:
✦ upload to Arduino a sketch, compiled and stored in a Raspberry Pi folder;
✦ manage clock and alarm;
✦ open a console Arduino;
✦ off the system.

In future articles, we will deepen both the schematic and the software issues.

General structure of RandA

RandATop

1106_TopSilk

 

 

Let us now look at the general structure of the RandA board. As mentioned, communication between Raspberry and RTC (DS1339) is via I2C. While the connection with the button circuit is via pin GPIO24 (input). Arduino, then, is connected to Raspberry through its pin D2 (output) and the GPIO22 pin (input); and through its RESET pin (input) and pin GPIO23 (in output).

With the first link, Arduino can draw the attention of Raspberry, through an active listener process on it, to make sure that you put Raspberry listening on the serial port and communicate with Arduino.

The second connection is made necessary since the USB circuit is missing, which create an automatic management of the RESET associating it to the DTR signal produced by the simulation of the serial port.

Finally, to turn on Raspberry when Arduino is working as controller, this is connected optionally via pin D4 (programmed as output with pull-up) to the button.

 

Fig8

 

 

RTC module

The clock is made from a DS1339 that interacts with Raspberry Pi via an I2C. To be precise, the DS1339 is seen at 0xFFD … (slave address) of the I2C protocol. Remember that in the I2C protocol, the last bit of the address varies according to the mode (read or write); so 0xFFD0 or 0xFFD1.

The two lines SCL and SDA of the I2C bus interface Raspberry through two bidirectional voltage adapters made by the two MOS T6 and T5 (see diagram). In fact, the DS1339 is powered at 5 volts, while Raspberry Pi, as you know, is fed at 3.3 volts. This handy bidirectional voltage adapter system has already been used in other shield and refer to these for the explanation of the operation.

The DS1339 contains a clock (date and time) and a pair of programmable alarm clocks, of which only one is used by our software. The DS1339 we fed with the voltage of 5 volts; but if this should fail, there is a button battery of 3 volts that maintains the operation in stand-by. The circuit of intervention of the buffer battery is included in the DS1339.

The alarm can be programmed on elapsed minutes or hour and minutes, or finally, on the basis of the day (of the month or week), hour and minute.

Once programmed the alarm, you have to activate it. When this happens, the output 7 of U2 (DS1339) is set to the high value (5V) and then turns off the MOS T7 (P-channel) that blocks the power supply.

When the alarm is triggered, brings to low value (0V) output 7 of U2 and then activates the MOS T7 that provides power.

Unfortunately, when the alarm is disabled, it is not put to a constant low value but to a condition of square wave generator with different programmable frequencies. This has forced us to insert a high-cut filter on output 7 of U2, so that the maximum frequency of 32.765 kHz corresponds to constant low level and then maintains the power T7 MOS enabled.

In essence, when Raspberry is turned on, it disables the alarm keeping the power and then sends a reset of the alarm by placing it at a low value (all zeros). In this way, a subsequent shutdown of the system will be definitive, and it will switch on manually. Unless, during operation, the alarm is not being re-programmed.

In fact the system shutdown occurs by enabling the alarm; operation that has the effect to turn off the MOS T7. At this point you can manually turn on the alarm bypassing it through the on-off circuit (see next chapter), or waiting for the time expiration, if the alarm clock is set.

The clock software management is performed by the library “wiringPi” installed on Raspberry. This open source library, in addition to Raspberry GPIO pins management also contains the driver for the I2C bus and the C ++ library to use it.

It was also installed the library “pi4j” that allows you to drive the GPIO pins and the I2C bus with the Java language.

 

Blocking power supply with power on and off

The 5 V power supply through USB connector can be supplied from any power supply, standard for any mobile phones and smartphones. If you want Raspberry and / or Arduino to support devices with heavy absorption, we must opt for power supply capable of 2 or 3 amps. If not, it is enough the standard power supplies by 1A.

Also you can feed the system with 5V even via a connector terminal (5Vin), in parallel with the microUSB, useful for example, in the event of a UPS or a power supply without USB output.

A switch (SW1) can power up, regardless of the alarm switch or other software, if you want to power the system always on.

The power is not just about Raspberry, but also Arduino and any shield to it added. It is chosen, however, the way of separating the two power supplies, so as to allow Arduino, optionally, to be always powered. This is an option that can be activated using a switch (SW2) that separates the two power circuitry.

Since Arduino has not its classic linear voltage regulator and its USB circuit, it consumes very low and could be used as a “switch” to turn Raspberry on or off. You can turn it on and off either by setting alarm or directly through its pin D4 connected to the power button. The switching may, therefore, be subject to other events in addition to time, as a condition on the digital or analog Arduino pin.

Or, finally, Arduino can be connected to the same power of Raspberry and being subject to the same on or off conditions.

One of the main characteristics of RandA is the electronic circuit that allows software power control. This circuit is made of: the P-channel MOS T7 which acts as a switch, the alarm clock of the RTC and U4 (a timer 555) that works as a flip-flop.

The MOS T7, with very low “on” resistance and capable of withstanding 3A and more current, is activated when its gate is at low level (<2V), while it is off when it is at high level (5V).

About alarm we have already spoken, while U4 is used to manage power via button (P1) remotized through the connector PUSH in parallel to which it can be connected another button, perhaps attached to a case. Even LD3 and LD4 are replicable by connecting external LEDs on the corresponding connectors. The LD3 (red) indicates the presence of power, while the led LD4 (yellow) is active only during the startup of Raspberry and turns off when this has completed its initialization and is ready to operate.

When the system is off and the button is pressed, the timer 555 triggers the flip-flop and puts port “out” at high level. The high output port puts in conduction T8 which, crossing as a diode the base of T9, puts a low level to the gate of T7 and activate it.

To reactivate the button and make it ready for shutdown, the flip-flop has to be reset and this is dealing with T9. In fact if Raspberry, using a command at startup, disables the alarm, drives to low the gate of T7 in another way. Then T9 is no longer enabled and puts the 555 threshold to high state, thus resetting the flip-flop.

At this point a new pressing of the button will activate again the flip-flop and will activate also the discharge of 555 that, this time, being Rasberry on, may be intercepted by one of its active process dedicated to this, listening to the GPIO24 pin connected to the discharge. The background process (daemon) launches the shutdown command that will shut down the board.

The button works, therefore, both for power on and shutdown. A yellow LED is displaying the “startup in progress”. But as soon as the system has booted, automatically turns off, signaling that it is ready to accept Raspberry login. In fact, the LED is powered by the current flowing in T8 and T9.

If Arduino were connected independently to power source via the switch SW2, it can directly  turn on or off Raspberry driving pin D4. Indeed D4 is connected in parallel to the button.

 

Module Arduino

As mentioned, RandA hosts a version of Arduino Uno deprived of its power system and the USB interface. Its programming must, therefore, be made through Raspberry or through network. On Raspberry it was installed the Arduino IDE for Linux. Through the Arduino IDE, you can program and load (uploading) the sketch compiled. The IDE can be launched from a linux console remotely with SSH, but being a graphical environment requires that the console can handle a server Xwindow. As we will see in the next episodes, an excellent software for this use is “MobaXterm”. Otherwise you must connect with the keyboard and monitor to Raspberry.

But there is another possibility to program Arduino with remote access: using the Arduino IDE for PC, which we modified to handle a remote serial port. This IDE launched on PC allows you to see any Arduino remotized and supported on Raspberry. To achieve this effect was also necessary to install on Raspberry a software that communicates with the IDE, and manages the serial port connected with Arduino as if it is another port accessible from the IDE, open on the PC connected to a local network.

Because RandA uses the direct serial connection with Raspberry, you must have the ability to activate the reset through a pin of Raspberry. Indeed Arduino standard software reset is made by the USB interface that is now missing. The USB interface circuit which simulates a serial connection transforms a DTR signal in a reset pulse. We can get the same result, with RandA, using the pin GPIO23 of Raspberry.

Even in the case of the links between Raspberry and Arduino we have the same voltage adjustment problem that we had met with U2 (RTC). T1, T2, T3, T4 and T10 achieve this separation between the levels of Arduino (5 volts) and those of Raspberry (3.3 volts).

In particular T1 connects GPIO23 with the Arduino RESET, while T2 and T3 interface serial lines TX (D1) and RX (D0).

T4, however, allows Arduino to require the attention of Raspberry by pin D2 connected to GPIO22. Indeed  on Raspberry is installed a background process that detects the change of state of this pin and triggers an additional process that interacts via serial with any Arduino sketch that uses a library designed to send commands executed by the Linux process. This way Arduino can command Raspberry. Conversely Raspberry, by commands, can install on Arduino sketches compiled and stored on its “file system”,as we will see in the next episodes dedicated software environment. A further optional connection (via bridge) connects the button on / off with the pin D4 Arduino, with the interposition of T10, to allow it to turn on and off Raspberry. Both T1 and T10 working with pulses because they are activated via capacitors. The rest of RandA is standard Arduino, with all connectors, quartz, RESET button, etc.

 

Raspberry Pi

RandA overlaps Raspberry Pi Plus using an intermediate frame necessary to anchor the board because it was not possible to use all four holes on Raspberry because of the compliance with both the Arduino standard of expansion connectors and the Raspberry connector link and especially the desire to respect the size of Raspberry Plus.

This way RandA and Raspberry Pi + form a very compact system, while maintaining the ability to use the Arduino shield.

RandA is also compatible with the previous version of the Raspberry Pi.

 

RandA is not only Arduino mounted on Raspberry, but wants to be an integrated system equipped with a power management and a programmable clock. This will make it easy to implement applications that turn on and off the system based on external conditions or the timing. With the addition of a buffer battery, maybe supported by a solar panel, and a WiFi key or a module with a GSM modem, it is possible to create a complete and powerful control and remote management system.
The potential of the Arduino expansions combined with the effectiveness of the multitasking Raspberry operating system and its connectivity, together with a complete power management, lets us imagine a multitude of possible applications.

 

Github Files

Here you can find all files

 

From the store

RandA

Raspberry Pi

About ddan