PICAXE-20M2 to controll 8 Servos

MariaPerez

New Member
Hi,
I need some help!! I want to control 8 servos using 8 toggle switches using a 20M2. I am using a CD4021 to read the 8 toggle switches and the 8 servos are connected to portB. I am struggling with the connections for the CD4021, I am thinking of using C), C1 - kb data and C2 - kb clk.

Any help would be appreciated.

Cheers folks!! :eek:

Maria P
 

premelec

Senior Member
Welcome to this forum . it would help to be more specific as to what you are trying to accomplish - the 4021 is a shift register rather than a multiplexer and does the toggle switch simply enable signals to the servos? [turn them on or off]. You imply you are trying to control 8 servos from one port... this is not so easily done... you can control 4 servos from an 08M2 directly... there are multiple servo control boards that might save you a lot of time as well. How fast do your servos have to react? Are servos really required or could motors work? More info please...
 

BESQUEUT

Senior Member
To read data, simply use the shiftin command.
Assuming you use b0 to read data, you can access individual bits with
bit0,bit1,bit2,...

you can control 8 servos from an 20M2 directly
 

erco

Senior Member
Welcome Maria. You could read 8 switches directly on port C pins, while port B drives 8 servos if that's all you need. 100% pin utilization on a 20M2.
 

premelec

Senior Member
@MariaPerez - if the toggle switches are to just control what servos are on you can also use a switched resistor network and single READADC in pin to discern which switches are on... such as is done with R-2R networks. Hope we aren't giving you too much information not in harmony with your original query. In any case note that it's quite possible to do with the 20M2.
 

MariaPerez

New Member
Hi All,
Thanks for your help so far. It is for a project for one of my students. She wants to control 8 servos using a 20M2, this should not be a problem. The issue is the limited I/O. She found some help on the Arduino site using a CD4021 to read the toggle switches and send the data using 3 pins serially. We are having problems translating it for use with a 20M2, we are unsure which pins to use, PORTB will be used for the 8 servos. She wants to use the remaining I/O for other things, this is why she cannot use 8 in and 8 out pins.

Any help will be greatly appreciated.
Thanks :eek:
Maria P
 

AllyCat

Senior Member
Hi,

... use a switched resistor network and single READADC in pin to discern which switches are on... such as is done with R-2R networks.
If any combination of switches might be closed at the same time (i.e. 256 permutations), then a single-input resistor network might be rather a struggle to get working well (needing resistors matched to around 0.2%) and of course use READADC10. If only one switch is closed at a time, there are many "keypad decoder" threads on this forum, using either resistors (to one ADC pin) or a "strobed" row-column multiplex (but the multiplex probably needs 6 pins).

However, two ADC pins each for 4 switches, using either a R-2R "ladder" network or simple "weighted" resistors (e.g. 1k5, 3k, 6k2, 12k) should work fine for any combination of switch positions. Note that resistor networks behave differently depending whether the switch inputs are "Open/Closed" or "Logic High/Low". An open/closed weighted resistor network needs a load resistor on the input pin (to ground), perhaps 1k if you don't mind calculating/measuring the non-linear spacing between the threshold (discrimination) levels, or a lower value load to avoid that.

But if you're committed to the CD4021, it can be done with a PICaxe, just tell us which package style and PICaxe pins you want to use.

Cheers, Alan.
 
Last edited:

MariaPerez

New Member
Hi,
Thanks. She really needs to use the CD4021, the bit we are struggling with is deciding which lines to use to interface to the CD4021 PORTB is used for servos.
Any advice would be greatfully appreciated.
Many thanks,
Maria P :p
 

AllyCat

Senior Member
Hi,

You can use almost any three Port C pins, but the "input only" c.6 can be best for input as it can't be used as an output.

So define three pins with symbol commands, e.g. SYMBOL Q8 = pinc.6 : SYMBOL P_S = c.5 : SYMBOL CLK = c.4 , which I think correspond to legs 3, 9 and 10 on the 4021 and legs 4, 5 and 6 on the 20M2.

Start the program with LOW CLK : HIGH P_S which should load the register (it doesn't seem to be necessary to pulse the clock to load a 4021)

Now use LOW P_S and then a FOR .. 1 TO 8 : .... : NEXT loop to read the Q8 value into a variable, e.g. BIT0 = Q8 for variable B0, then shift the 4021 with a PULSOUT CLK,1 . To ensure that you end up with the correct bits within B0, that needs to be shifted before each reading of Q8, e.g. with B0 = B0 * 2 (or B0 = B0 + B0).

Cheers, Alan.
 

MariaPerez

New Member
Hi,

You can use almost any three Port C pins, but the "input only" c.6 can be best for input as it can't be used as an output.

So define three pins with symbol commands, e.g. SYMBOL Q8 = pinc.6 : SYMBOL P_S = c.5 : SYMBOL CLK = c.4 , which I think correspond to legs 3, 9 and 10 on the 4021 and legs 4, 5 and 6 on the 20M2.

Start the program with LOW CLK : HIGH P_S which should load the register (it doesn't seem to be necessary to pulse the clock to load a 4021)

Now use LOW P_S and then a FOR .. 1 TO 8 : .... : NEXT loop to read the Q8 value into a variable, e.g. BIT0 = Q8 for variable B0, then shift the 4021 with a PULSOUT CLK,1 . To ensure that you end up with the correct bits within B0, that needs to be shifted before each reading of Q8, e.g. with B0 = B0 * 2 (or B0 = B0 + B0).

Cheers, Alan.
Thanks ;)
Maria P
 

AllyCat

Senior Member
Hi,

Which 330R resistors, where? :confused:

The 8 parallel inputs to the 4021 will require pull-up or pull-down resistors (depending on whether the switches are grounded or go to the supply rail) but 330R is unusually low.

Cheers, Alan.
 

MariaPerez

New Member
Hi,

Which 330R resistors, where? :confused:

The 8 parallel inputs to the 4021 will require pull-up or pull-down resistors (depending on whether the switches are grounded or go to the supply rail) but 330R is unusually low (especially for a PICaxe).

Cheers, Alan.
Oops, my bad!

The resistors between PORTB and the servos, I see those used a lot with PICAXE but not for example with Arduino, I am wondering if they are needed, is this something particular to PICAXE?

Thanks,
Maria P :)
 

AllyCat

Senior Member
Hi,

No, I don't think they're essential. They're for short-circuit protection on the assumption that wiring mistakes may be made in an "educational environment". But generally, I'd expect a PICaxe to be more "forgiving" of wiring mistakes than an Arduino. :)

Cheers, Alan.
 
Top