IR Sensor

Here you can learn how t o use the ADC of external ports.

(This example is using the IR sensor for explanation. )


Things to Prepare

CM-510 or 700

IR Sensor (Connected to the external sensor No.1)



Analog signals can be changed into digital values through the micro controller.  Through this example, the analog voltage values of external IR Sensor, Gyro Sensor etc. can be read.

PORTA is used in the example.  Please refer to Controller Port Map.




ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // ADC Enable, Clock 1/64div.

ADMUX = ADC_PORT_1; // ADC Port 1 Select

This part is for initialization to use serial communication.  The serial initialization function is included in serial library, and the serial port is initialized if communication speed is transmitted by data.

In the case of sei(), it is an internal command makes users possible to use "Interrupt."

Besides that, there is a register setting for ADC control.  For the control, please refer to the datasheet of Atmega2561.

The datasheet can be downloaded at


PORTA &= ~0x80; // ADC Port 1 IR ON


_delay_us(12); // Short Delay for rising sensor signal

ADCSRA |= (1 << ADIF); // AD-Conversion Interrupt Flag Clear

ADCSRA |= (1 << ADSC); // AD-Conversion Start


while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete


PORTA = 0xFC; // IR-LED Off


printf( "%d\r\n", ADC); // Print Value on USART


This part is for analyzing and transmitting the analog signals detected through PORTA.

First of all, turn on the IR LED 1 of PORTA, and then start converting the amount of received light into digital values.

When the conversion is complete, turn off the IR LED, and then transmit the values through serial communication.



When it is connected to PC in serial, IR sensor values are printed on the screen.