CS684: Embedded System Course
Lab 3: ADC Interfacing
LEARNING RESOURCES
This lab requires understanding of sensors interfacing with AVR microcontroller and basic knowledge about Analog to Digital Conversion. Refer the Resources file.
AIM
In this lab, you will interface ADC with the micro-controller. The aim of this lab is to get you familiar with ADC present on ATmega2560.
In this lab, you will interface the White Line and IR Proximity sensors. Your task is to get the 8-bit ADC result from the three white line sensors and 5th IR proximity sensor in Single Conversion Mode and display the ADC converted digital values on UART Serial Terminal.
The program is provided as Eclipse project. But, the program contains few incomplete functions which you would have to complete as per the instructions present in the comments.
CONNECTIONS
- Left White Line Sensor : PC1 [ ADC Channel 1 ]
- Center White Line Sensor : PC0 [ ADC Channel 0 ]
- Right White Line Sensor : PC2 [ ADC Channel 2 ]
- 5th IR Proximity Sensor : PC5 [ ADC Channel 5 ]
PROCEDURE
Step-1: Download lab3_adc zip file. Right-click on the hyperlinks and select Save Link As... option to download. Extract the zip file. Start Eclipse, click on File --> Open Projects From File System --> Directory and browse for lab3_adc folder to open the project.
Step-2: In src/eBot_Sandbox.cpp, complete the send_sensor_data function to achieve the following:
Read data from 3 white line sensors and IR proximity sensor. Print IR proximity sesnor data on the terminal.
Note: While completing, you have to use following functions defined in eBot_MCU_Predef.h header file:
- unsigned char convert_analog_channel_data( unsigned char sensor_channel_number );
- int print_ir_prox_5_data(unsigned char);
Step-3: Build the project (click on Project --> Build Project). If there are no errors present in your program, the project will get build successfully and you will get the message (on console at bottom pane) as shown below,
**======== Build Finished. 0 errors, 0 warnings ========**
Step-4: Check the behaviour of the robot on CoppeliaSim.
- Download the lab3_adc.ttt scene. Start CoppeliaSim, click on File --> Open Scene and browse for lab3_adc.ttt file to open the scene (shown in Expected Output - CoppeliaSim video).
- In Eclipse, right click on lab3_adc folder (Project Explorer - left pane) and select Run As --> Local C/C++ Application. If no errors are encountered, following message will be displayed onto the console (bottom pane):
Connection Success ...
0
0
0
Please Enter Y to Start Simulation: Y
Enter 'Y' on console. This will start the simulation in CoppeliaSim and following message will be displayed on the console:
Simulation started correctly.
Initialized all sensors in the current CoppeliaSim scene.
Enter 'Y' / 'y' to take the next sensor reading or 'Q' / 'q' to quit: y
You can check the reading of IR proximity sensor by entering 'Y' / 'y'. Change the position of the robot (in CoppeliaSim) and observe the readings (in Eclipse).
Note: To change the position of the robot (in CoppeliaSim) click on object/item shift and enter X and Y co-ordinates in position tab.
To change the orientation of the robot, click on object/item rotate and enter alpha, beta and gamma values in orientation tab. (Refer EXPECTED OUTPUT - CoppeliaSim video)
- Verify the readings for following position and orientation:
pos(X,Y) - (0.2020, 0.2000), ori(Alpha, Beta, Gamma) - (0, 0, 90) --> reading(127)
pos(X,Y) - (0.2020, -0.2000), ori(Alpha, Beta, Gamma) - (0, 0, -90) --> reading(127)
pos(X,Y) - (0.6060, -0.2000), ori(Alpha, Beta, Gamma) - (0, 0, 0) --> reading(70)
pos(X,Y) - (-0.6060, -0.2000), ori(Alpha, Beta, Gamma) - (0, 0, 90) --> reading(73)
Step-5: You will notice some pre-written function stubs in src/eBot_MCU_Predef.c included for your assistance related to motor and timer. Complete the pre-written functions with the help of comments provided.
Step-6: In the Package Explorer (left pane), right click on lab3_adc folder and select Show in Local Terminal --> Terminal (bottom pane). Type the following command in the terminal:
avr_hex_file_generator -cpp src/lab3_adc_main -dcpp src/eBot_Sandbox -dc src/eBot_MCU_Predef -m atmega328p
If there are no errors present in your program, the project will get compiled correctly and you will get the message as shown below, Also, build folder will be generated in project directory that will contain lab3_adc_main.hex file along with other build files.

Step-7: Load the hex file on the SimulIDE circuit (AVR_simulator.simu) once your program gets build successfully. Save the project. Ensure that code performs as expected. NOTE: Getting Build succeeded output doesn't mean that your program will give the expected output as it can contain logical errrors.
MACROS to use
- In the skeleton code eBot_MCU_Predef.c we have included a header file named, eBot_MCU_Predef.h which consists of the declaration of the following constants (for atmega328p controller):
// For 3 White Line Sensors
#define wl_sensors_ddr_reg DDRC
#define wl_sensors_port_reg PORTC
#define left_wl_sensor_pin 1 // PC1
#define left_wl_sensor_channel 1 // ADC1 - ADC Channel 1
#define center_wl_sensor_pin 0 // PC0
#define center_wl_sensor_channel 0 // ADC0 - ADC Channel 0
#define right_wl_sensor_pin 2 // PC2
#define right_wl_sensor_channel 2 // ADC2 - ADC Channel 2
// For 5th IR proximity Sensor
#define ir_prox_5_sensor_ddr_reg DDRC
#define ir_prox_5_sensor_port_reg PORTC
#define ir_prox_5_sensor_pin 5 // PC5
#define ir_prox_5_sensor_channel 5 // ADC5 - ADC Channel 5
- You have to use these constants declared in eBot_MCU_Predef.h and the Masking Operators to complete the lab.
EXPECTED OUTPUT - CoppeliaSim
Software required: CoppeliaSim and Eclipse (refer Installation_Instructions provided to you)
EXPECTED OUTPUT - SimulIDE
Software required: SimulIDE AppImage (refer Installation_Instructions provided to you)