Interfacing GY 26 With Atmega640

Introduction: Interfacing GY 26 With Atmega640

Hello friend once again with new instructable in this I will show you how to interface GY 26 with UART when I started working on this very less resource where availble over internet .Thus i thought to share my piece of work
.GY 26 is a digital compass which gives angle with respect to earth north .it can be used for robot orientation and position aero model position and so on. GY26 uses UART or IIC for communication with microcontroller . communicating with GY 26 is simple and straight forward .

Step 1: Understanding UART in AVR

In AVR there are five register

1) URR-is used to set the baud rate for serial communication which is agreed by both the devices which is given by X=(F_CPU/16(desired baud rate))-1
2)USCRA-has flag of all USART interrupts
USCRB-this register is used to enable yhe interrupt
USCRC-this is the control register which is used toset the mode synchronous or asynchronous ,parity bits,stop bits,character size and clock polarity.
3)UDR-data registor

the timing is very critical issue check the frequency at which your board is running

here is a site for USART

Step 2: Understanding GY26

parameters of serial communication for GY26
Baud rate: 9600bps
Verify bit: N
Data bits: 8
Stop bit: 1

***Interfacing with gy26 in USART is simple and straight forward to get the angle we have to send a command 0x31 on this GY26 will return 7 bytes as follow

(1)Byte0:0x0D (ASCII: enter)
(2)Byte1:0x0A (ASCII: new line)
(3)Byte2:0x30~0x33 (ASCII: hundreds of angle 0~3)
(4)Byte3:0x30~0x39 (ASCII: tens of angle 0~3)
(5)Byte4:0x30~0x39 (ASCII: bits of angle 0~3)
(6)Byte5:0x2E (ASCII: decimal point of angle)
(7)Byte6: 0x30~0x39 (ASCII: decimal of angle)
(8)Byte7: 0x00~0xFF (calibrate sum)

Byte7= the lower 8 bits of (Byte0+ Byte1+ Byte2+……Byte6)
<0x0D-0x0A-0x33-0x35-0x39-0x2E-0x36-0x1C> = 359.6°

Different commands in GY26 are-
(1)0x31: measure the angle (return the value of the angle)
(2)0xC0: start calibration
(3)0xC1: end calibration
(4) 0xA0-0xAA-0xA5-0xC5: return to the settings of the factory
(5) 0xA0-0xAA-0xA5-IIC_ADDR: change the IIC bus address
(6)0x03-DECL_high: set the high 8 bits of declination angle
(7)0x04-DECL_low: set the low 8 bits of declination angle

send 0xC0 to the module, it return <0x0D-0x0A-0x30-0x30-0x30-0x2E-0x30-0x05>, which means starting calibration successfully

you can get the datasheet here:

Step 3: Connection of GY26 With Microcontroller


the code is simple and straight forward
1)USART in AVR is initialized for communicating with gy26
Baud rate: 9600bps
Verify bit: N(no parity bits)
Data bits: 8
Stop bit: 1
2)then command 0x31 is send to get angle using function USART_Transmit()
3)then USART_Receive( ) is used to read the data from GY26

Be the First to Share


    • Puzzles Speed Challenge

      Puzzles Speed Challenge
    • "Can't Touch This" Family Contest

      "Can't Touch This" Family Contest
    • CNC Contest 2020

      CNC Contest 2020

    4 Discussions


    5 years ago
    Sunil93 please contact...


    Reply 5 years ago

    No.... I want to contact you for accompany with me in working on cpu designing....


    Reply 5 years ago

    I can't say all here... So contact me. if u want ...