PICAXE 18M2 test board
Fig. 1 Schematic to my home built test board minus I/O resistors.

PICAXE 18M2 Servo Control

by Lewis Loflin

The three simple routines below go further than the PICAXE manual. The programs are self explanatory.
For more on the operation of hobby servos see Basics of Hobby Servos.

connect a servo
Fig. 2 how to connect a servo. Connect to pin B.0.

Program 1


#rem

Page references pertain to PICAXE Manual 2 Basic Commands

#endrem

; uses command 'servo'

symbol pulse_pin = B.0
symbol val = b0

init: servo pulse_pin,75  ;initialize servo

main: 

    readadc control, val ;read ADC1 8-bit mode into variable b0 p. 170
    val = val * 1  MIN 75 MAX 225 ; set limit on val 
    servo pulse_pin, val
    pause 20
  
goto main   ;loop back to start

10k pot  connection for potentiometer     picture of potentiometer
Fig 3 connecting a potentiometer. Connect wiper to C.1.

Program 2 Position with Potentiometer

Related: also see Potentiometers and Analog-to-Digital Conversion with the PICAXE


; uses pulsout command (p. 161), uses no servo commands
; uses a potentiometer to position a servo

symbol pulse_pin = B.0
symbol control = C.1 ; position control wiper
symbol val = b0

LOW pulse_pin

main: 
    
    readadc control, val ;read ADC1 into variable b0 p. 170
    val = val * 1  MIN 75 MAX 225 ; set limits on val
    pause 19
    pulsout pulse_pin, val ; val * 10uS at 4mHz
 
goto main   ;loop back to start

Program 3 Sweep Back and Forth



; Operate a servo sweep back and forth.

symbol pulse_pin = B.5
symbol val = b1

init: servo pulse_pin,75  ;initialize servo

main: 

   for val = 75 to 224 
    servo pulse_pin, val
    pause 20
   next val
    
   for val = 224 to 75 step -1
    servo pulse_pin, val
    pause 20
   next val
    
goto main   ;loop back to start


Picaxe Micro-controller Projects!

The PICAXE series of micro-controllers rank as the easiest and most cost effective way to use Microchip processors. I wanted an easier and less expensive way to introduce my students to the "PIC" micro-controller. Here I hope to get those starting out past poorly written literature and lack of simple working code examples.

See How I got into Electronics

 


donate