Showing posts with label mcu. Show all posts
Showing posts with label mcu. Show all posts

Monday, June 3, 2013

ATmega has got a new friend: BeagleBone!

What happens when two awesomesness meet together?

Well, we get 2x AWESOMENESS :)

So, I've got the BeagleBone to talk to my AVR ATmega328, and here is how, from the scratch up to running.


Here is a list of what I used:


  




BeagleBone board - (http://beagleboard.org/Products/BeagleBone)
STK500 Board - (http://www.atmel.com/tools/STK500.aspx)
Sparkfun's Logic Level converter - (https://www.sparkfun.com/products/8745)
A mix of jumper wires

-----------------------------
Although I have used the ATMEL's STK500 board, you can use any other programmer
-----------------------------

I am not an expert or any thing close to it, I am writing what I did and learned for the convenience of other people


Step 1: Installing Ubuntu 13.04 "Raring" on the BeagleBone

source 1: elinux  Wiki (http://elinux.org/BeagleBoardUbuntu)
source 2: GigaMegaBlog (http://www.gigamegablog.com/2012/09/03/ubuntu-on-the-beaglebone-enabling-analog-in-pwm-i2c-and-spi/)


1.1 - On a linux machine, follow the following tutorial http://elinux.org/BeagleBoardUbuntu#Raring_13.04_armhf

the last step would be:


sudo ./setup_sdcard.sh --mmc /dev/sdX --uboot bone

note the red X, this should be replaced with the correct letter according to what ID your PC assigns to the SD card

e,g,:

sudo ./setup_sdcard.sh --mmc /dev/sdg --uboot bone



After finishing, the SD card should be ready, take it out from the PC and plug it in the beaglebone.


Connect your beaglebone to your router using an Ethernet cable, this is needed for downloading software and for easy access

Connect your beaglebone to a power source (for now, use the mini-USB port and connect it to your PC)

wait until Ubuntu loads up, it takes about a couple of minutes

using Putty, we can access the beaglebone to issue commands, this needs an SSH-server enabled on the beagle.



try to connect by choosing the options as above:

Connection type: SSH
Host Name: arm

if it connect, that is awesome, and you can skip the following section.

if it fails, we'll need to connect through serial connection:


On a windows PC, Check your device manager

On a Ubuntu machine, check the contents of the devices directory

ls /dev


check before and after connecting your beaglebone, you will find that an extra serial port is available, open that port in a serial terminal at 115200 baud rate, and hit the tiny reset button on the beaglebone (next to the 4 user LEDs),

You will see the system booting up, wait until it is ready and then do the following:
source: https://help.ubuntu.com/12.04/serverguide/openssh-server.html


sudo apt-get install openssh-server


sudo /etc/init.d/ssh restart


The SSH server should be up and running now and you should be able to remote connect using putty with the settings mentioned above.

The default username and password are
ubuntu
temppwd

That is, if you have not changed the password, if so please do so as soon as possible since your beagle is now on the internet with an SSH door

sudo passwd





Step 2: The I2C interface
source: http://www.gigamegablog.com/2012/11/04/beaglebone-coding-101-i2c/

You have one usable I2C interface, that is i2c-3, on BeagleBone System Reference Manual PG. 59, it is refered to as I2C2_SCL and I2C2_SDA (Port 9:19, 9:20)


First, let us check the I2C interface:


sudo i2cdetect -r -y 3

The output should be something like:



Let us connect some wires now, but first we should shutdown Ubuntu and unplug the USB cable

sudo shutdown -h now


wait for 5~10 seconds till the system shutdown, then unplug the USB cable

connect the circuit as shown below (modified picture from sparkfun (https://www.sparkfun.com/products/8745)):

Also, while you are connecting, connect an LED to PB0 and another to PB1 on the ATmega



Download and unzip the example code from:
https://github.com/MuazSalah/BeagleBone_ATmega
https://github.com/MuazSalah/BeagleBone_ATmega/archive/master.zip

Remember to change the makefile according to your hardware

Compile and program your ATmega, it will be assigned as an I2C slave at address 0x0A


Power up your BeagleBone again, after about 20 seconds, you will be able to SSH to it, when so, issue the command


sudo i2cdetect -r -y 3




download and unzip the example code:



wget https://github.com/MuazSalah/BeagleBone_ATmega/archive/master.zip

sudo apt-get install unzip

unzip master.zip

cd BeagleBone_ATmega-master/BeagleBone


run the python code:


sudo python LEDs_Blink.py










Additional resources:

Enabling file sharing (NAS) on the BeagleBone
http://elinux.org/R-Pi_NAS

This a link for a tutorial on Raspberry PI, however, it is exactly similar to BeagleBone except that the service (or deamon) name is smbd and not samba, so you say

sudo /etc/init.d/smbd restart

and not

sudo /etc/init.d/samba restart

This is very useful to view your files on your linux/windows machine

------------------------------------------------------------------------------------

Final Notes:

1- I2C read is not working
2- Although the address is configured as 0x0A on the ATmega, the BeagleBone reads it
as 0x05, a factor of 2 which seems to be constant at different slave addresses


Logic Analyzer data:

* Write operation:

* Read Operation:








EDIT:


Finally, I found a way to read through I2C using the function readList(startAddress,Length) from the same Adafruit library, if I need to ready 1 byte, I should get 2 bytes from the I2C, e.g.

readList(0,2)

This will read 2 bytes from I2C, the first byte will be the I2C receive address, and the second will be byte[0] on the txbuffer of the I2C slave



Time to RoboCar the BeagleBone



Sunday, January 1, 2012

How Fast Relays Are?

From the time I learned about relays, I wondered if they can be used to make an H-Bridge for motors, I asked my professor and he said that you can't because they are slow.

I never knew how slow, till today, so I wrote a small code on my AVR ATmega168 microcontroller

basically, the code send an ON -> delay(x) -> OFF -> delay(x)

where x is a value (in ms) changing every time a button is pressed going down from 15 ms to 1ms

1:  #include <avr/io.h>  
2:  #include <util/delay.h>  
3:  #include <USART.h>  
4:  #define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))  
5:  #define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))  
6:  int main(void)  
7:  {  
8:       init_uart();  
9:        SETBIT(DDRB,0);  //Output pin (ON/OFF)
10:       CLEARBIT(DDRB,1); //Input to change time
11:       SETBIT(PORTB,1);  //Enable Pull-Up resistor
12:       for (;;)  
13:       {  
14:            for (uint16_t i=15; i>0; i--)  //repeat 15 times going down, used as delay
15:            {  
16:                 printf("i=%d\n",i);  //just to see whats the current delay value
17:                 _delay_ms(100);  //give me time to release button
18:                 while ((PINB & 0x02)==0x02)  //loop when I don't press button
19:                 {  
20:                      SETBIT(PORTB,0);   //ON
21:                      _delay_ms(i);      //i value as delay
22:                      CLEARBIT(PORTB,0); //OFF 
23:                      _delay_ms(i);
24:                 }  
25:            }  
26:       }  
27:       return 0;  
28:  } 

And then connected the relay, driving it with a NPN



I found that min time is 2ms, at 1ms, no switching will occur ( i was getting highly rippled 5v)



and here is an awesome sound of 1~15ms switching, at 0:16 the time is 1ms



~2ms (500Hz) is the min time (max freq) u can switch a typical relay




Friday, November 11, 2011

NetHome

I am extremely happy today, I am almost done with Light Control through Ethernet. I can't believe it was soooo easy!

I started this project about a week ago, not a new idea or any thing, it is for my own educational purpose

NetHome

I started with a rough sketch of the idea (pictures are hi res so it will take time to load)


After getting the required components I started implementing directly



5v power is supplied from USB, 3.3v regulator is used for the webserver WIZ200Web from Sparkfun

Relay PCB, to control the 220 VAC


Powering up!


Yay its working, now feedback sensor, Miniature Solar Cell



WIZ200Web - web server (from sparkfun)

Using this guy was awesome, only need to connect two 3.3v pins, four GND pins, Rx0, Tx0. Then from PC use configuration tool to connect/configure and download firmare/webpages.



Below, I was testing in external ATmega168 before using the server (which has ATmega128 with pins you can use for I/O ADC etc)




Ethernet link - blinking!



webpage:




I've connected my relay driver to PB4 pin (on J2) which is LED0 on the WIZ200web development board, so I was able to control the light.


After that I decided to make two interfaces, one from PC "PC Station" and an "Android Station" and maybe also "iPhone Station"

I started with the simplest, "PC Station" for which I used Visual Basic:






The PC Station is working fine, however, there is small problems with changing the IP Address, if the connection is successful and I tried to change the ip address, it would still say "connected"
Also, I've not implemented Syncing data yet

Since It is working, I thought I'd move to the next step, Android!

The UI on Virtual Manager





I am able so far to toggle the picture ON/OFF, however, I am still looking for how to control the light :D


.