Servo motor is a type of motor that rotate to specific degree assigned by a controller. Some motor rotates within 180 degree, while others rotate up to 360 degree.
A sample of servo motor from Ali Express
This is a high torque motor that could handle some heavy load especially robot arm. It has three wires - two for supply voltage while the remaining one is control signal.
Supply voltage is typically a +5V DC supply from microcontroller board. Its control signal is a TTL type logic level with a frequency of about 50Hz. Duty time of control signal is between 1.5ms to 2ms that rotate its angle between 0 and 180 degrees.
Microcontroller Interfacing and Programming
Controller needs to generate Pulse Width Modulation (PWM) signal with a frequency around 50Hz, with a variation of high time between 1.5ms to 2ms to rotate this motor. Without using PWM module of controller, programmer can create a software PWM routine instead. However this method has a timing latency.
Simulation of this program
A POT connects to AN0 of PIC16F887 that's channel 0 of ADC module. Controller reads and converts this ADC value into timing value of output PWM signal generates a pin RD0. This pin controls the angle of rotation of servo motor.
Input signal to ADC channel of a microcontroller is other than DC voltage source. It typically range from 0 to 5V DC, as a conventional microcontroller, and voltage reference of ADC module are around +5V. Measuring input voltage within this range is straight forward using a calculation of ADC result and its step voltage size.
A DC +50V DVM using ADC of PIC16F887 MikroC - Program sample
Embedded controller is able to measure input DC voltage exceeding this range - for example measuring an input voltage of +50V DC. From electric circuit theory, voltage divider circuit divides input voltage source to lower output using two resistors.
Reference circuit from Wikipedia
Dividing factor depends on value of these two resistor that we can not give its detail and formula here. Using this method we can design a simple digital volt meter (DVM) with a maximum range of +50V DC.
In this example we select Z1 = 100kOhm and Z2 = 10kOhm. This circuit gives an output dividing factor of 0.09 at Vout. So whenever input voltage Vin = 50V then output voltage 50V * 0.09 that is around 5V, which is the maximum input voltage to ADC input channel.
Circuit Diagram
In this program timer0 is used to schedule ADC reading before it's converted to actual DC input voltage. Using its 8MHz internal oscillator source, timer0 interrupt creates a timer tick for scheduling ADC reading. Whenever the total tick equal to 30 it's a one second timing. Hence ADC reading is activated for every 30 timer0 tick (one second).
However multiplexing display doesn't use this timer tick due total lines of C source code.
LM35 is thermometer that converter surrounding temperature to analog voltage. Its analog voltage voltage is proportional to temperature that is 10mV per degree Celsius. It's supplied from a DC voltage source ranging from 4 to 30V. Conversion temperature is between -55 to 150 degree Celsius.
Sample of this example program
Dual-In-Line Package of TO-92 is very popular for hobbyist prototyping of this kind of sensor. It's a three-pin transistor-like electronics device. These three pins are VDD, VOUT, and GND.
LM35DZ TO-92 transistor-like package
From electronics component store this device costs below one USD.
Interfacing and Programming using MikroC
Using ADC module inside PIC16F887, temperature reading could be done easily and precise due to 10-bit resolution inside ADC module. Floating point calculation in program C allow result acquisition calculated within a line of C code.
Circuit Diagram
In this programming example, AN0 of PIC16F887 reads voltage value that represent temperature data. Conversion temperature in only positive since voltage reference of ADC here set to positive rail only.
Temperature data will show on multiplexed display driven by PortB. Device supplied from stabilized 5V DC voltage with 8MHz internal oscillator.
Voltage=ADC_Value*STEP_SIZE;// Convert to Decimal [Volt]
}
ADC_RUN=0;
}
void SSD(){
Temp_Data=Voltage/Volt_Per_Celcius;// convert Voltage to integer
PORTB=LED[Temp_Data/10];//10's
DIGIT1=1;
delay_ms(RATE);
PORTA=0x00;
PORTB=LED[Temp_Data%10];// 1's
DIGIT2=1;
delay_ms(RATE);
PORTA=0x00;
PORTB=0x63;// o
DIGIT3=1;
delay_ms(RATE);
PORTA=0x00;
PORTB=0x39;// C
DIGIT4=1;
delay_ms(RATE);
PORTA=0x00;
}
void main(){
ADC_Value=0;
int_count=0;
ADC_RUN=0;
PORT_SETUP();
ADC_Init();
Timer0_Setup();
Interrupt_Setup();
while(1){
Read_Temp();
SSD();
}
}
Timer0 is added to this example program. It's used for creating timer ticks that schedule ADC reading, and activating each digit of multiplexing display.