Hello everyone,
I’m glad to take you through the procedure involved in interfacing an Servo motor with ATmega 16/32 microcontroller board. Servo motor can be bought from our online store by clicking here
Hardware Prerequisites:-
- ATmega 16/32 board.
- Servo motor: You can avail this module easily from our online store
- AVR USB programmer:-USBasp. This is also available in our online store by clicking here
- CP2102:-USB to TTL serial converter. Avail this from our online store
- Jumper Wires for making connections.
Software Prerequisites:-
- AVR studio for coding and compiling
- AVRdude for burning/uploading hexcode
- PuTTy:- A terminal software.
ATMEGA 16
FEATURES
- ATmega16 is an 8-bit high performance microcontroller from the Atmel’s Mega AVR family.
- Atmega16 is a 40 pin microcontroller based on enhanced RISC architecture with 131 powerful instructions.
- It has a 16 KB programmable flash memory, static RAM of 1 KB and EEPROM of 512 Bytes.
- Most of the instructions execute in one machine cycle.
- It can work on a maximum frequency of 16MHz.
SERVO MOTOR
- Servo is a motor that we can command to go to particular angle.
- it has three pins
- power pin(normally red)
- ground pin(normally brown)
- output pin(normally orange)
- it will turned between 0 degree to 180 degree
STEP 1:
Make clean connections for interfacing Servo motor with ATmega 16/32 board by following these pin inter-connections.
SERVO MOTOR PINS | ATMEGA 16/32 PINS |
V | 5V |
A0 | PB5 |
D0 | keep open |
GND | GND |
Below shows the picture used to interface the Servo motor with ATMEGA 16/32
STEP 2:
Open AVR studio. Copy the program source code along with its attachment libraries and headers. Save and Build the main program source code in AVR studio.
To set board and frequency go to Project>Configuration options in AVR studio. Select device as ATmega16/32 and set Frequency as 8000000 Hz
STEP 3:
Connect USBasp USB programmer to ATmega 16/32 board. Open AVRdude software. In configuration settings, select ATmega16/32 as microcontroller.
STEP 4:
In memories section of AVRdude, browse for hexcode generated for the main source code
STEP 5:
Also, check if the write button is ticked. If not ,tick it. After uploading hexcode. Press execute to burn the code into ATmega 16/32 board.
If everything is uploaded perfectly, then Servo rotates from 0 degree to 180 degree and vice versa
SAMPLE CODE TO CHECK SERVO MOTOR #define F_CPU 8000000UL #include <avr/io.h> #include <util/delay.h> #include "servo_motor1.h" #include "main.h" int main(void) { init_hardware_pwm1(); while(1) { change_speed_pwm1(upper_bound); _delay_ms(2000); change_speed_pwm1(lower_bound); _delay_ms(2000); } return 0; } void increase_speed_pwm1(int increment) { if ( Motor_PWM1 + increment <= upper_bound) Motor_PWM1 = Motor_PWM1 + increment; else Motor_PWM1 = upper_bound; } void decrease_speed_pwm1(int decrement) { if (Motor_PWM1 - decrement >= lower_bound) Motor_PWM1 = Motor_PWM1 - decrement; else Motor_PWM1 = lower_bound; } void change_speed_pwm1(int speed) { Motor_PWM1 = speed; } void init_hardware_pwm1(void) { TCCR1A |= (BIT7|BIT5|BIT1); // configuration for timer on compare mode for pwm configuration TCCR1B |= (BIT4|BIT3|BIT1|BIT0); ICR1=2499; // selecting pwm period to 20 ms DDRD|=(BIT4|BIT5); // Pwm out to output PORTD4 and PORTD5 }
where is servomotor.h?