Sony Arouje

a programmer's log

Archive for April 23rd, 2015

RS485 Communication Protocol with Micro Controllers and Raspberry pi

with 8 comments

We are in the age of connected devices, devices could talk each other either via RF or wired. In one of my post I explained about Radio Frequency communication using Xbee. In this post lets see how devices can talk each other via wired network. One of the advantage of Wired network is, it is cost effective compare to buying an XBee modules like Xbee pro. I personally prefer Wireless communication as it’s hassle free, just place the devices where ever we need.

In my previous post I explained how to Bootload Atmega16A and program it using Arduino. Next step is to establish the communication between the Micro controllers and Raspberry pi. In this case my Raspberry Pi is acting as the central hub that talk to other controllers and collect data or issue command to do some job.

RS 485 Protocol

I better leave it to Wikipedia to give a detailed explanation of the protocol. In brief using RS485, devices can communicate Full duplex or Half duplex. I used a Half duplex communication. So it’s a protocol, how do we implement the protocol in our hardware, there is a chip Max485 from Maxim. This chip can establish a half duplex communication.

Max485 Pin configuration

Max 485 is a 8 pin chip as shown below. It’s a DIP chip and SMD have different pin layout.

image

Image from Maxim site

Pin 1 – RO: It’s the Data-In pin, devices can read data from the bus using this pin. Rx pin of the Micro controllers should connect to this pin.

RE and DE: Set this pin to Logical High to transmit any data to the bus. Set this pin to Low to receive data from the bus.

Pin 4 DI: Devices can transmit data via this pin.  This pin should be connected to the Tx pin of the device.

Pin 8 and 5 – VCC and GND: To power the chip, VCC should be 5 volt.

Pin 7 and 6 – A and B: Here we connect the data line. A should connect to the A Pin of the next Max 485 and B with B.

 

Connection Diagram

The below picture will illustrate how to connect all the devices together.

RS485_SerialCommunication_bb 

Image created using Fritzing

As per the diagram, if RPi want to transmit any data. We should set a high voltage on GPIO 18 and issue a serial write. All other devices in the network will be in listening mode and can read data using serial read, if any other device wanted to transmit data, then issue a Logical High to RE DE pin and will get promoted as a master and transmit data via serial write.

Now it’s up to you to program it and do some cool things with it.

Written by Sony Arouje

April 23, 2015 at 11:05 pm

Bootloading Atmega16 with Arduino Uno

with 19 comments

Last two days I was experimenting with Microcontrollers and researching about how to write applications on it. I bought one Atmega16A and decided to use Arduino bootloader. So that I can write programs using Arduino IDE and syntax. This post explains about how to bootload Atmega16. I didn’t had any prior experience with Arduino, I have a uno sitting at my desk for quite a long time but never used it. Last three nights I was learning more about Arduino coding and learning about boards config and other stuffs.

Software Setup for Bootloading

I followed the steps posted by Sudar in his blog, please go through the post before reading further. Here I will write some points I had to do to do to complete the bootloading process.

Arduino IDE version: To run the core created by Sudar, we need the IDE version 1.0.x. I have the latest version 1.6.3. There are some differences in configuring boards.txt from 1.0 to 1.6. I tried to rewrite the boards.txt as per 1.6.* standard but it didn’t like it. So I downloaded the old version 1.0.6 from Arduino website. If you have 1.6.* already installed then better download the zip file and extract to a folder. That way you can use 1.6 and 1.0 in same machine.

Adding Boards and Variants: Adding boards to the sketch book folder didn’t work for me, may be I didn’t get it clearly. So I did the below steps.

  1. Go to the Arduino’s hardware folder, <<IDE install location of IDE>>/hardware/arduino/ and open boards.txt.
  2. Copy the contents of Atmega16 boards and pasted to the boards.txt opened in Step 1.
  3. Create a new folder named mega16 under <<IDE install location>>/hardware/arduino/variants and paste the “pins_arduino.h” created for Atmega16.
  4. Open Arduino IDE 1.0.* and goto Tools->Boards and you should see as shown belowarduino_ide_v1.0.6If if you can see the new board under Boards menu as shown above then we successfully setup the software environment for bootloading.
  5. Goto File->Examples->ArduinoISP, it will open a new window with ArduinoISP sketch. Before uploading the sketch make sure you have selected Arduino Uno in Tools->Boards->Arduino Uno. Now upload the sketch to your Uno. Now we setup our Uno as a programmer.

Hardware Setup for Bootloading

Here again I followed the pin connection detailed in Sudar’s post. Please follow exactly the way mentioned in the post. Also don’t forget to connect the capacitor between Reset and GND of Arduino.

Note: If you try to upload any sketch to Arduino Uno when the Capacitor is connected, IDE will throw errors. So before doing any thing with your Arduino remove the Capacitor.

Bootloading

Follow the below steps to burn the bootloader

  1. We have to set our Arduino Uno as an ISP. To do that go to Tools->Programmer and select “Arduino as ISP”
  2. Set the bootloader from Tools->Boards and select the newly added board, ie. Atmega16(internal 8mhz clock).
  3. Now burn the bootloader by selecting “Burn Bootloader” under Tools menu.

If every thing works fine, you could see a success message at the bottom portion of the IDE.

Uploading Sketches to Atmega16

Next we need to upload some sketches to newly bootloaded Atmega16. For this I used the new version of Arduino IDE 1.6.3, If you want you can use IDE 1.0.*. Some how the old IDE is very slow for me, so I decided to use the new version. Skip the below step if you decided to use the old 1.0.* version.

Follow the steps to use the Atmega16 board in new version

  1. Create a new folder under <<Installed location of IDE V1.6.*>>/hardware. You can name the folder anything, I named as Mega16.
  2. Under Mega16 folder create another folder called “avr”.
  3. Inside the avr folder copy the boards.txt and create another folder named variants and copy “pins_arduino.h” inside variants folder.
  4. Open boards.txt and add the line “atmega16-8.upload.tool=arduino:avrdude”

Follow the below steps to upload sketch(Common for all versions of IDE)

  1. Open up the IDE and load Blink from File->Examples->Basic.
  2. Edit the code so that you are writing to a pin of Atmega16, here I write to Digital 6, it’s physical pin 20.
  3. Before uploading the sketch Choose Atmega16(internal 8 Mhz) from Tools->Boards.
  4. Choose “Upload using Programmer” from File menu.

Just connect a LED with a Resistor to Physical pin 20 of Atmega16 and you can see the LED blinking.

 

Now I can write any thing to the Atmega16 and connect to sensors or any thing I wanted to do without Arduino.

Written by Sony Arouje

April 23, 2015 at 1:42 pm

%d bloggers like this: