Figure 1: Arduino compatible board
Introduction
At this time, you must be wondering what Alamode is all about? It is a shield designed by Wyolum team for Raspberry PI.
To understand in a much simpler way, it’s a shield which 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:
- ATMEGA328P 8bit microcontroller
- Precision real time clock (RTC)
- Micro SD card reader
- Reset button
- 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 which are required for the Raspberry PI and Alamode communication using 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 is 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 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 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:
- In built high precision real time clock with an integrated temperature-compensated crystal oscillator.
- Supports built in micro SD card reader. You can make use of it to read and write some useful information.
- One can make use of the alamode analog pins and program with Arduino sensors.
- Supports all Arduino Uno and standard compatible shields.
- In build GPS support for Fastrax UP501 module.
- 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 Rasbian operating system. If you wish to download the latest rasbian, follow this
link.
Run the following commands to install arduino on Raspberry PI.
- $ sudo apt-get update
- $ sudo apt-get install arduino
Now it’s the time to setup 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 setup the alamode. Coming next you will understand.
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.
- $ tar -xvzf alamode-setup.tar
Navigate to alamode setup folder and run the setup program.
- $ cd alamode-setup
- $ 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 about behind 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 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 communicates 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 on
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 is 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 master and the connected device such as Arduino as 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 console and also in addition to that the getty, a program that display 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.
In order to disable gitty, comment the following line with a ‘#’ sign in front of it.
- #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.
- console=ttyAMA0, 115200 kgdboc=ttyAMA0,115200
Finally you will have to reboot 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 does 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.
- sed -i'bk' -e's/console=ttyAMA0,115200.//' -e's/kgdboc=tty.*00.//' /boot/cmdline.txt
- sed -i'bk' -e'/T0.*ttyAMA0.*vt100/s/T0/#T0/' /etc/inittab
- 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 for getting the board information for uploading the HEX code. The avrdude makes use of the upload.speed as a baudrate for uploading code.
- cp avrdude /usr/bin/avrdude
- cp avrdude /usr/share/arduino/hardware/tools
- cp avrdude.conf /usr/share/arduino/hardware/tools
- cp boards.txt /usr/share/arduino/hardware/arduino
Here’s the snippet of the Alamode board info.
- alamode.name=AlaMode
-
- alamode.upload.protocol=alamode
-
- alamode.upload.maximum_size=32256
-
- alamode.upload.speed=115200
-
- alamode.bootloader.low_fuses=0xff
-
- alamode.bootloader.high_fuses=0xde
-
- alamode.bootloader.extended_fuses=0x05
-
- alamode.bootloader.path=optiboot
-
- alamode.bootloader.file=optiboot_atmega328.hex
-
- alamode.bootloader.unlock_bits=0x3F
-
- alamode.bootloader.lock_bits=0x0F
-
- alamode.build.mcu=atmega328p
-
- alamode.build.f_cpu=16000000L
-
- alamode.build.core=arduino
-
- alamode.build.variant=standard
Creating dev rules for Alamode so that we are naming the devices.
- KERNEL=="ttyAMA0", SYMLINK+="ttyS0",GROUP="dialout",MODE:=0666
- 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 setup permissions to run the avrdude.
- cp 80-alamode.rules /etc/udev/rules.d
- chown root /usr/bin/avrdude /usr/share/arduino/hardware/tools/avrdude
- chgrp root /usr/bin/avrdude /usr/share/arduino/hardware/tools/avrdude
- 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 be 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.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void setup()
- {
-
- pinMode(13, OUTPUT);
- }
-
-
- void loop()
- {
- digitalWrite(13, HIGH);
- delay(1000);
- digitalWrite(13, LOW);
- delay(1000);
- }
Under Tools -> Board select “Alamode”.
Then select the serial port and “
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
- 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 any issue with the Alamode but I lately realized the fact that the GPIO pins of Alamode was 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.
- 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.
- 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 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.