Alamode And Raspberry PI

RaspberryPI
 
Figure 1: Arduino compatible board
 

Introduction

 
At this time, you must be wondering what Alamode is all about? It is a shield designed by the Wyolum team for Raspberry PI.
 
To understand in a much simpler way, it’s a shield that sits on top of Raspberry Pi. The Alamode interfaces with the Raspberry Pi via I2C, SPI or Serial UART. The heart of Alamode is ATMEGA328P-PU, which is an 8-bit Microcontroller with a 32KB In-system flash runs on 20MHz with a voltage of 1.8V-5.5V.
 
Alamode is an Arduino compatible board or a shield for Raspberry Pi. The alamode includes the following:
  1. ATMEGA328P 8bit microcontroller
  2. Precision real-time clock (RTC)
  3. Micro SD card reader
  4. Reset button
  5. Standard shield headers
Those who have programmed with Arduino would have understood by this time, the alamode is nothing but an Arduino compatible clone. The alamode microcontroller is loaded with an Arduino Uno (optiboot_atmega328.hex) boot loader.
 
We will see in brief about the underlying things about the alamode. Since it‘s based on the Arduino platform, you have to code the Arduino sketches and then upload the same to alamode. We will be setting up the alamode to install Arduino IDE and then we will have a look into other configurations that are required for the Raspberry PI and Alamode communication using the serial interface.
 
If you are curious to know the program which is responsible for uploading the hex file contents of your sketches to Arduino, then you will have to take a look into the link for avrdude documentation. Yes, it is the avrdude, an executable which is a program for downloading and uploading the on-chip memories of Atmel’s AVR microcontrollers which are also being used by alamode.
 
The alamode uses 5v power, you can either go with external power supply or it can be powered from Raspberry PI. There is an option for that and one should be careful in enabling the 5V link option. The alamode can be programmed in a similar way that one used to do with Arduino. Coming next, we will see how to set up and use alamode and program the same.
 
Note: When you are dealing with a low power application, then you can power the alamode through Raspberry Pi. Say if you are working on a simple LED project, then it’s good to go with the Raspberry Pi’s internal power. If you are connecting the shield and working on some complex projects, make sure to power up the Alamode with a 5V power supply.
 
We will be discussing the following mentioned points about Alamode:
  • Why Alamode?
  • Setting up Alamode for Raspberry PI
  • Behind the scene of Alamode
  • Demo Code
  • Common Problems
  • Why Alamode?
Let us see why you want to go with alamode. The following are some of the key things that I have listed, it is useful in many ways:
  1. Inbuilt high precision real-time clock with an integrated temperature-compensated crystal oscillator.
  2. Supports built-in micro SD card reader. You can make use of it to read and write some useful information.
  3. One can make use of the alamode analog pins and program with Arduino sensors.
  4. Supports all Arduino Uno and standard compatible shields.
  5. In build GPS support for the Fastrax UP501 module.
  6. Interface with Raspberry PI via I2C, SPI or Serial UART.
More information about the Alamode can be found at:
Setting up Alamode for Raspberry PI
 
Let us see how to setup alamode for Raspberry PI (Model B or B+).
 
The alamode works with Noobs or the Rasbian operating system. If you wish to download the latest rasbian, follow this link.
 
Run the following commands to install Arduino on Raspberry PI.
  1. $ sudo apt-get update  
  2. $ sudo apt-get install arduino  
Now it’s the time to set up an alamode package. Run the below mentioned command from your terminal. What we are trying to do is, connect to git repository and get the required setup files for alamode. There is a reason why we have to set up the alamode. Coming next you will understand.
  1. $wget http://git.io/alamode-setup  
Once the above command runs successfully, you should be able to see the alamode-setup.tar file which we will have to extract using tar command.
  1. $ tar -xvzf alamode-setup.tar  
Navigate to the alamode setup folder and run the setup program.
  1. $ cd alamode-setup  
  2. $ sudo ./setup  
The setup runs within a second. You will have to restart Raspberry PI to see the changes.
 
Behind the scene of Alamode
 
Before we start discussing the scene of Alamode, there are various configuration changes that I am going to discuss. You don't really have to worry about these configurations as it's all taken care of by the Alamode setup program.
 
As I have mentioned previously, the Alamode's heart is the same Arduino Uno microcontroller. We will see how the Alamode communicates with the Raspberry PI through serial UART. For those of you, who have some experience or understanding in connecting Raspberry PI to Arduino, you wouldn’t be much surprised in the way it works. Still, I am putting together some of the aspects that might help in understanding the way how the Alamode works with Raspberry Pi.
 
We will not get into the details about the board schematics etc. but let us try to understand the high-level things which might be beneficial to you in understanding the reason behind setting up the alamode. Or say you want to know how the Raspberry PI and alamode communicate with each other, you will have to understand the basics about UART (Universal Asynchronous Reception and Transmission).
 
Feel free to have a look into the following link to have some understanding of UART.
 
Note: The alamode supports I2C, SPI or Serial UART when you are interfacing with Raspberry Pi. But it makes use of Raspberry PI’s Serial UART for uploading sketches.
 
Before we dig into the details, let us see how the Raspberry PI supports communication with the external device.
 
Here’s the typical interfacing mechanism that you see in connecting two devices and communicate with each other by sending and transmitting data to and fro using TX and RX connections.
 
The serial interface in Raspberry PI consists of two signals the TX and RX which are used for transmitting and receiving bits. In order to communicate with an external device with serial communication over UART, you will have to connect the TX of Raspberry PI to the RX of the connected device and similarly, the RX of the PI has to be connected to the TX of the connected device such as Arduino.
 
Note: The TX and RX are a part of the GPIO pins in Raspberry PI.
 
It might look very simple, but there is something which we have to understand that is, the Raspberry PI runs with a voltage level of 0 and 3.3V logic levels whereas the Arduino runs with a voltage level of 0 to 5 volts.
 
There is a voltage difference between these two devices and you wouldn’t be able to directly connect the TX and RX of each of these devices. You need an additional circuitry that is the voltage divider or a logical level convertor. However, when you are dealing with I2C communication with Raspberry PI as a master and the connected device such as Arduino as a slave, you don’t need a voltage divider.
 
Let us now see how the alamode makes use of Raspberry PI’s serial UART for communication. Note - The Raspberry PI uses the UART as serial console input/output. We will have to disable in using a console and also in addition to that the Getty, a program that displays login screen has to be disabled too. You don’t really have to do any of these when you are using alamode for Raspberry PI. The reason being, when you are running the Alamode setup, it does everything for you.
 
Edit the /etc/inittab file to disable gitty. You can do that by running the following command.
  1. $sudo nano /etc/inittab  
In order to disable gitty, comment on the following line with a ‘#’ sign in front of it.
  1. #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100  
When the Raspberry Pi boots up, by default it sends data over the serial port. Now we have to disable the same from using console so that the alamode can use it for communicating with the Raspberry Pi. For that, you have to remove the following information from /boot/cmdline.txt.
  1. console=ttyAMA0, 115200 kgdboc=ttyAMA0,115200  
Finally, you will have to reboot the Raspberry Pi.
 
We have now understood the communication between Raspberry Pi and Alamode. There is more to know.
 
When you are running the Alamode setup, if you open up the setup file you will notice below lines which do exactly the same I mentioned above for disabling the serial console for ttyAMA0 from boot -> cmdline.txt and also removes an entry in etc/inittabbk.
  1. sed -i'bk' -e's/console=ttyAMA0,115200.//' -e's/kgdboc=tty.*00.//' /boot/cmdline.txt  
  2. sed -i'bk' -e'/T0.*ttyAMA0.*vt100/s/T0/#T0/' /etc/inittab  
  3. rm /etc/inittabbk  
The following things are part of the Alamode setup. When you are running the setup, it copies the “avrdude” and the associated configuration file “avrdude.conf” to the required directories. You will see next, the reason why we have to copy these files.
 
The boards.txt is the file that avrdude program looks into forgetting the board information for uploading the HEX code. The avrdude makes use of the upload.speed as a baud rate for uploading code.
  1. cp avrdude /usr/bin/avrdude  
  2. cp avrdude /usr/share/arduino/hardware/tools  
  3. cp avrdude.conf /usr/share/arduino/hardware/tools  
  4. cp boards.txt /usr/share/arduino/hardware/arduino  
Here’s the snippet of the Alamode board info.
  1. alamode.name=AlaMode  
  2.   
  3. alamode.upload.protocol=alamode  
  4.   
  5. alamode.upload.maximum_size=32256  
  6.   
  7. alamode.upload.speed=115200  
  8.   
  9. alamode.bootloader.low_fuses=0xff  
  10.   
  11. alamode.bootloader.high_fuses=0xde  
  12.   
  13. alamode.bootloader.extended_fuses=0x05  
  14.   
  15. alamode.bootloader.path=optiboot  
  16.   
  17. alamode.bootloader.file=optiboot_atmega328.hex  
  18.   
  19. alamode.bootloader.unlock_bits=0x3F  
  20.   
  21. alamode.bootloader.lock_bits=0x0F  
  22.   
  23. alamode.build.mcu=atmega328p  
  24.   
  25. alamode.build.f_cpu=16000000L  
  26.   
  27. alamode.build.core=arduino  
  28.   
  29. alamode.build.variant=standard  
Creating dev rules for Alamode so that we are naming the devices.
  1. KERNEL=="ttyAMA0", SYMLINK+="ttyS0",GROUP="dialout",MODE:=0666  
  2. KERNEL=="ttyACM0", SYMLINK+="ttyS1",GROUP="dialout",MODE:=0666  
The setup has the following information for copying the dev rules and setting up the permissions for avrdude so that when you are uploading Arduino sketches, behind the scene it uses the avrdude. You definitely have to set up permission to run the avrdude.
  1. cp 80-alamode.rules /etc/udev/rules.d  
  2. chown root /usr/bin/avrdude /usr/share/arduino/hardware/tools/avrdude  
  3. chgrp root /usr/bin/avrdude /usr/share/arduino/hardware/tools/avrdude  
  4. chmod a+s /usr/bin/avrdude /usr/share/arduino/hardware/tools/avrdude  
Within the Alamode, you will notice the avrdude and avrdude.conf. These are nothing but the modified version of the actual program used for uploading code from Arduino. Ideally, the Arduino IDE has no knowledge about the Alamode. If you open up the avrdude.conf, has the programmer for Alamode which will be used by avrdude while you are uploading the sketches.
 
Running the setup program for Alamode overwrites the files so that when you open up the Arduino IDE, you will be able to select the board, programmer and since we are enabling serial port, you should also be able to select the serial port for uploading code.
 
Demo Code
 
Let us now walk through with an example to understand how you can use Alamode with Raspberry PI. We will be using the simple blink sketch which comes with the Arduino samples.
 
Open up Arduino IDE in Raspberry PI and use the following code for testing your alamode to see how it works by blinking an LED on PIN 13 with a one-second delay.
 
Here’ the code snippet of blink sketch.
  1. /*  
  2. Blink  
  3. Turns on an LED for one second, then off for one second, repeatedly.  
  4. Most Arduinos have an on-board LED you can control. On the Uno and  
  5. Leonardo, it is attached to digital pin 13. If you're unsure what  
  6. pin the on-board LED is connected to on your Arduino model, check  
  7. the documentation at http://arduino.cc  
  8.   
  9. This example code is in the public domain.  
  10.   
  11. modified 8 May 2014  
  12. by Scott Fitzgerald  
  13. */  
  14. // the setup function runs once when you press reset or power the board    
  15. void setup() 
  16. {  
  17.     // initialize digital pin 13 as an output.    
  18.     pinMode(13, OUTPUT);  
  19. }  
  20.   
  21. // the loop function runs over and over again forever    
  22. void loop()
  23.  {  
  24.     digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)    
  25.     delay(1000); // wait for a second    
  26.     digitalWrite(13, LOW); // turn the LED off by making the voltage LOW    
  27.     delay(1000); // wait for a second    
  28. }  
 Under Tools -> Board select “Alamode”.
 
Alamode
 
Then select the serial port and “dev/ttyS0”.
 
dev/ttyS0
 
Compile or upload the sketch which will compile and upload the HEX code for our blink sketch to Alamode. You should be able to see the LED blinking at PIN 13.
 
Note: There is a small LED onboard in Alamode on D13, which should start glowing with a 1s delay.
 
Common Problems
 
These are some common problems as stated on the following website,
  1. One common problem which you might encounter while uploading the sketch is:
     
    avrdude: ser_recv(): programmer is not responding.
     
    avrdude: stk500_recv(): programmer is not responding.
     
    This happens for various reasons. If you wish to know more information about the problem, under Arduino preferences, you can set the show verbose output during upload so that should give more information about the problem. I had encountered a similar issue, the microcontroller was unable to respond during the upload and there wasn’t an issue with the Alamode but I later realized the fact that the GPIO pins of Alamode were not rightly in place with the Raspberry Pi. There was a slight gap and I happened to slant the Alamode a bit so that the Raspberry PI’s GPIO is fully in contact with the Alamode.
     
  2. There are cases where the serial port is in use, in such cases, you won’t be uploading the sketch. Make sure the serial port /dev/ttyS0 is not in use while you are uploading the sketch.
     
  3. There are times you are unable to select the serial port from Arduino IDE. 
Note: This should not happen if you have installed the Alamode correctly. But if it happens don’t worry; you can manually update the boot/cmdline.txt and /etc/inittab as I have explained earlier.
 
Points of Interest
 
I am not an expert in the electronics field and neither it’s my specialization. What I have mentioned in this article is the result of my research and learning as a part of IoT. Please correct me if I am wrong or things need to be improved.
 
The alamode itself is an interesting interfacing shield for Raspberry Pi. This small shield has opened up ideas for building things in IoT as it brings us more opportunities to use a microcontroller to program and deal with various sensors and shields that one can connect to it. One can simply build applications both on Raspberry PI microprocessor and Alamode’s microcontroller and communicate with each other through Raspberry PI’s GPIO serial interface. What was missing in Raspberry PI is all provided through this small little shield - The Alamode.