Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

connecting servo to a PIC

Status
Not open for further replies.

TheMaccabee

New Member
hello
Im trying to drive a servo motor with PIC16f877a
Can I connect the PIC pins directly to the servo??or is there should be a buffer or a resistor in between the servo trigger pin and the PIC pin??If so plz tell me why??
 
Here’s a rather simplified example

**broken link removed**


Some other considerations; have a 100uF+ capacitor on the output of your PIC's voltage regulator, and a 0.1uF tantalum capacitor between both the +Ve and -Ve of your PIC's power pins.

Your servo may have a different rated power supply, not sure.. Also, some servo's will create a lot of noise, perhaps an opto-isolator would come in handy?
 
You don't want to connect a hobby type servo to 12V. Their maximum rating is 6V. I posted some code in this thread showing how to control a servo with a pic.

Mike.
 
Pommie said:
You don't want to connect a hobby type servo to 12V. Their maximum rating is 6V

Like I said, not sure what servo he is using, so It would depend on that.

Servo's must have their desired position updated every 50mS (20Hz), and the signal is simply a pulse (refer to your datasheet of your servo), but generally 1mS to 2mS long

If your using PIC Basic, its as simple as

Repeat

Code:
	 Repeat

	 	 SERVO PORTA.0, Servo_Position	 	 
		 DelaymS 48
		 
		 Inc Servo_Position
		 
	 Until Servo_Position = 2000
	 
	 Repeat

	 	 SERVO PORTA.0, Servo_Position	 	 
		 DelaymS 48
		 
		 Dec Servo_Position
		 
	 Until Servo_Position = 1000


This will rotate a servo, and then rotate it the opposite way. Notice I delay for 48mS between pulses; this is to compensate for any code overheads to ensure the servo is being updated at least 20Hz or better.
 
Last edited:
gramo said:
Servo's must have their desired position updated every 50mS (20Hz), and the signal is simply a pulse (refer to your datasheet of your servo), but generally 1mS to 2mS long

I thought they had to be updated every 20mS(50Hz).

Mike.
 
It doesn't actually matter a lot. as long as you update it you're good. i have done it from 50Hz to 1Hz and it still worked... just get the pulse right. some take 1-2ms, some 0.75 to 2.25ms... find the manual and examine it closely.
 
blueroomelectronics said:
Hey Pommie, nice work on your servo code for the Firefly. People love it. Can I post it on my site? (credit to you of course)

Of course. I wrote it as a demonstration of how easy things were to get up and running with a Firefly.

Mike.
 
TheMaccabee said:
hello
Im trying to drive a servo motor with PIC16f877a
Can I connect the PIC pins directly to the servo??or is there should be a buffer or a resistor in between the servo trigger pin and the PIC pin??If so plz tell me why??

A resistor in series with the line can afford you some protection against accidently shorting the signal line to ground or Vcc (or anything else). I'm not sure what protection your PIC has, but if its like a lot of others, you could damage the output driver on that pin if you try to drive it externally. You'll also see a bit of filtering as a result of having it in there as well...
 
220ohms works like a charm.
Code:
;*** PWM_Servo.asm Firefly DIP switch UDDUDD
;*** uses VR1 to set PWM speed to control a servo
        list    p=16F88
        include <p16F88.inc>
        __CONFIG _CONFIG1, 0x377C & _CCP1_RB3 &_WDT_OFF
  
        org     0x000           ; reset vector
        movlw   b'00000100'        
        movwf   T2CON           ;B0 TMR2 on 
        movlw   b'00111100'     ; 
        movwf   CCP1CON         ;B0 CCP1 PWM mode
        movlw   b'11001001'     ;
        movwf   ADCON0          ;B0 A/D on VR1             
        bsf     STATUS,RP0      ;B1 change to bank 1
        movlw   b'00110010'        
        movwf   OSCCON          ;B1 500KHz oscillator & INTRC
        movlw   b'00000010'     
        movwf   ANSEL           ;B1 PORTA,1 (VR1) analog input
        movlw   b'11110111'
        movwf   TRISB           ;B1 RB3 Output (PWM)
        movlw   b'10011011'     
        movwf   PR2             ;B1 set PWM to 50.34 Hz
        
main    bcf     STATUS,RP0      ;B0 change to bank 0        
        bsf     ADCON0,GO       ;B0 begin A/D conversion (VR1) 
addone  btfss   ADCON0,GO       ;B0 A/D done yet?
        goto    addone          ; loop till A/D done
        swapf   ADRESH,W        ;B0 W = A/D high byte
        andlw   b'00001111'     ; mask off high nibble
        movwf   CCPR1L          ;B0 set duty cycle to A/D result
        goto    main            ; * any A/D result > 250 will = 100% PWM * 
        end
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top