Increase Outputs with Second Picaxe

bhanlon56

New Member
Hi,
I'd like to control two small motors with a MX1508 motor controller connected in turn, to a 08m2. To have enough inputs and outputs I was going to use a second 08m2 (as I have them) and connect them via a serial connection. The code on the master 08m2 (copied/modified from the manual1, page 106) is
Code:
main:
serout c.1,N2400, (%00000010)
pause 500
goto main
The slave 08m2 code is
Code:
main: 
serin c.3,N2400,b1
    let pins = b1
    debug b1
    pause 500
    goto main
I was hoping (after connecting c.1 of the master to c.3 of the slave) that the code would result in c.1 on the slave 08m2 going high. It does not work. The debug command shows no change in variable b1 of the slave 08m2. What am I doing wrong? Thanks.
 

AllyCat

Senior Member
Hi,

You probably need to "declare" pin C.1 as an OUTPUT (by default nearly all pins are Inputs at power-up). You could do that either with a LOW c.1 or a DIRSC = %00000010 near the top of the Slave program, and can be confirmed by using the Simulator.

Note that the b1 in the DEBUG command does nothing (all bytes are always reported). If you want to send just one byte (e.g. quickly) then use a SERTXD(#b1," ") or similar, which will be reported on the Terminal screen as a "decimal" number (i.e. not Binary or Hex).

Another word of warning; if you (instead) use SERRXD b1 (to receive a byte from the Terminal/Keyboard), it generates an "invisible" DISCONNECT which means you will then need to use the "Hard Reset Procedure" to send a new program to the PICaxe.

PS: And you have linked the EARTH rails of the two PICaxes haven't you? ;)

Cheers, Alan.
 
Last edited:

bhanlon56

New Member
Thanks Alan,
I added low c.1 to the master, not slave 08m2, assuming that a serin command would set c.3 as an input. I left out the debug command as I realized the programming cable was attached to the master 08m2. Still does not work. With %0000010 in the master (serout) command none of the slave pins are high when measured with a multimeter.
 

AllyCat

Senior Member
Hi,

Have you noticed my PS/EDIT above?

I think the SEROUT should set the Master's pin as an Output, but most of the Slave's pins default to Inputs at power-up (except the Serial/Programming output pin). So the Slave code will need a DIRSC = {pins bit image} or a LOW (or High) command for the required output pin(s) to make these pins Outputs. Does b1 change if you connect the Programming/DEBUG cable to the Slave connector ?

Cheers, Alan.
 

bhanlon56

New Member
I think I understand now. I added let dirsC=%00010111 to the slave code and I can control the outputs c.0, c.1, c.2 and c.4 using the master 08m2. Variable b1 didn't change if I transferred the programming cable to the slave 08m2. There was no data; it was "waiting". However the voltage on the pins varied between high and low as I changed the code on the master 08m2 from %00010111 to %00000000. I think its working.
Thanks for your help, really appreciated.
Brendan
 

Dartmoor

Member
Being a bit thick regarding micro processors, I have struggled on several occasions with serial data. There seem to be all sorts of issues regarding N/T, clock speeds /baud rate changing for different functions etc.
As I usually only need a small range of set speeds (eg 8), I recently gave up and used infra-red controls to send speed data.
You don't actually have to use infra red, just use the commands.
e.g. 1-8 (slow -> fast) & 0 for stop.
For reverse, A-H?
All the clever stuff is already done for you. It is obviously very limited but it solved my issues :)
Apologies if I am spreading bad habits, but it seems so much easier for a simple project.
 

bhanlon56

New Member
Never used the IR commands thinking they were for IR use only. Had a quick look at the irin/irout commands. So what you suggest, is for the master 08m2 to send c.0 on the slave 08m2 high is:
Code:
main:
irout c.4,1,0
goto main
And the slave:
Code:
main:
irin c.3,b1
let pins=b1
goto main
The slave 08m2 is connected to a mx1508 motor controller. Each motor needs two control connections (IN1, IN2 for motor1). If I connect c.0 and c.1 from the slave 08m2 to the mx1508, motor1 should turn on. To reverse the motor, I send irout c.4,1,1 from the master 08m2. This sends c.1 high and c.0 low? Have I got this right? I'm assuming the "device" number in the irout command doesn't matter.
 

AllyCat

Senior Member
Hi,
Apologies if I am spreading bad habits, ......
Hmm, IMHO the "Bad habit" is not giving an example of any functional Program code. :( To be honest, I'm not sure what "You don't .. have to use infra red, just use the commands. e.g. 1-8 (slow -> fast) & 0 for stop. For reverse, A-H? " actually means. There are 5 "Infra Red Commands", of which three are "deprecated"; I assume that IROUT and IRIN are proposed and I'm NOT going to say that they will not work (because I've never tried), but I do not expect them to work (correctly) because AFAIK IROUT transmits "Active-High" pulses modulated at 40 kHz, whilst IRIN expects to receive "Active-Low" unmodulated pulses (via an IR-receiver module).

It really shouldn't be difficult to get serial communications working between two PICaxe chips via a wire. You just need to ensure that the two chips are "talking the same language", i.e. P or N polarity, the same baud rate (taking into account any elevated clock rate, e.g. N2400_16 for SETFREQ M16) and that each BYTE is interpreted in the same way at each end of the link (e.g. 12 , "12" and #b12 are NOT the same). Also, there are a few well-known "gotchas" such as that you must link the Earth rails of the two chips, the (optional) "Qualifier(s)" must be used sensibly, and the transmitting program must allow sufficient time for the receiving program to process each Byte before sending another.

Thus the IR protocol may "assist" a novice by introducing a "forced" delay (each IROUT instruction takes around 20 ms) and by giving no options to choose Polarity, Baud Rate, Byte format or the main clock frequency. But those options may soon be needed to make any progress with a program, for example two motors each with 8 possible speeds and a "reverse" flag create 256 permutatations, which exceeds the capability of the IRIN command (128 codes). And of course it's quite common to use a "continuous" speed control via PWM, or a servo position for steering, etc., which require the transmission of at least full byte values.

Brendan: Your code proposal is quite plausible, but in practice you'd probably (need to) use something like irout c.4,1,b0 to efficiently change values, the Slave code needs a DIRS = $FF (or similar) near the top and (at least according to the manual) the "device number" should always be for the "TV Mode" (1).

Cheers, Alan.
 
Last edited:

bhanlon56

New Member
Thanks for the replies. I'll stick with with serout/serin as it works and for me, more intuitive. I've volunteered to teach a very basic micro-controller course at my local U3A. The serial link I think is easier to understand except that I'll have to introduce a bit of binary code.
 

AllyCat

Senior Member
Hi,

I had recollections that @erco had discussed the transmission of control signals via IR commands and his thread proved easy to find HERE. He used a Radio Transmitter/Receiver pair (for which it is often suggested can be "replaced with a piece of wire" for initial testing) and confirmed the need for a Logic Inverter to compensate for the (expected) Active Low input from the IR receiver/demodulator. Actually, it's a pity that Rev Ed didn't code the IROUT to be Active Low which has a higher drive current capability and would have permitted a direct wired connection for testing. However, the Active High does allow the use of an external NPN transistor to greatly increase the IR diode current and extend the range considerably. Also, my Full 12-bits IR decoder code snippet can be arranged to decode either Active-High or Active-Low pulses.

In his thread above, erco commented that he had not been able to transmit serial (RS232 ASCII) data through the RF modules, which is a well known issue because a large inbalance between the number of 0s and 1s can upset the bit-detection and/or the AGC (Automatic Gain Control) levels. However, there are 70 byte patterns which have an equal number (4) of "Highs" and Lows" and a further 56 bytes which (each) have a ratio of either 3:5 or 5:3 . Thus, if one can arrange a "gap" between characters of about one bit-period (i.e. the equivalent of "2 stop bits") then there are 126 codes which are only half a bit-period away from perfect symmetry (i.e. 5.5 : 5.5. including the Start and doubled-Stop bits). Of course there is no simple "pattern" to these 126 codes from the possible 256 for a byte, but these can be quickly handled by a Lookup Table, i.e. a pre-prepared READTABLE or READ of EPROM Data, with 126 entries for coding and 256 for decoding. Or a small number of codes might be coded with a simple LOOKUP command and decoded by a SELECT ... CASE structure. Where possible, the number of consecutive 1s or 0s should be also kept to a minimum.

Cheers, Alan.
 
Last edited:

erco

Senior Member
Thanks for the mention @AllyCat. Per that post and others, I have found that both the IROUT and RFOUT protocols are robust and less finnicky than SEROUT/IN. Setup & comms voodoo are already in the firmware and everything just works. I love a good workaround at every opportunity. Maybe one day I'll send RF out over an IR link to restore the balance in my universe.

@bhanlon56: Getting two Picaxes talking to each other is a LOT of work... the times I've done it, I had to have two laptops (one on the master and one on the slave) running the editor. Very frustrating until you get it to work, then it seems so easy. I see you're connecting a pair of 08M2s... and of course, sacrificing at least one pin on each for the data link. If space permits, shove a 14M2 or 20M2 in there and be done with it! Top 4 connections (power & programming) connections are the same, so even if you can't change the socket, you could leave the end hanging and solder (carefully) to the remaining pins. A bit ghetto but it would work.
 

Goeytex

Senior Member
Hi,

Adding a second Picaxe to get more I/O pins would probably my last option. I would only consider this if I were poor and broke, and could not afford a chip with more pins or...If I were using a 40 Pin chip and still needed more I/O's. And even then I would probably opt for an I2C I/O Expander IC instead of another Picaxe.

IMO the best option by far is to select and use a single microcontroller with enough I/O's to do the job. It will be much easier, less frustrating, faster, simpler, use less memory and ultimately be more reliable than using a second Picaxe.

Goey
 

lbenson

Senior Member
If space permits, shove a 14M2 or 20M2 in there and be done with it!
Adding a second Picaxe to get more I/O pins would probably my last option. I would only consider this if I were poor and broke, and could not afford a chip with more pins or...If I were using a 40 Pin chip and still needed more I/O's. And even then I would probably opt for an I2C I/O Expander IC instead of another Picaxe.
Agree and agree. And if you really want a lot of digital I/Os, there's this 45-LED Doll House Lighter with a 20M2 and 2 MCP23017s.
 

bhanlon56

New Member
A larger chip is absolutely the way to go. My supplier even has 20m2 in surface mount cheaper than 08m2 at the moment. Its just that I'm running the course for 8 retirees at U3A (university of the third age) group. The course is free for them. There's no budget for parts so I'm recycling bits from a previous life. I am poor and broke! Collecting enough parts to make 8 robot cars is a challenge. If the course runs again, I'll approach the U3A management to see if if they can subsidize consumables.
@ibenson, the school I worked at closed the electronics course I ran as there was no teacher interested in running it. I collected a lot of the stuff they were throwing out including a heap of LEDs. Had no idea what to do with them till I saw this:
https://picaxeforum.co.uk/threads/pwm-jellyfish.31894/
I thought I might do a similar thing. So I'll keep your idea for more I/O for then.
 

erco

Senior Member
Hi,
I'd like to control two small motors with a MX1508 motor controller connected in turn, to a 08m2.
Getting back to your original question:

MX1508 is a nice little dual H-bridge. Each H-bridge requires 2 outputs from the 08M2. You can also use pin C.0 (serial output programming pin) along with pins C.1, C.2 and C.4. That leaves inputs C.3 and C.5 (serial in, best limited to an active-low input) for control inputs. I usually use C.3 for an IR receiver.

Another option is to use continuous rotation servos instead of motors, which require just one pin for control using SERVO and SERVOPOS commands.

Finally, you could use my favorite Feetech board (unfortunately not always easy to source) which controls any two motors using just one pin each, essentially converting motors to continuous rotation servos. Video below.

Edit: 2 boards for $12.83 USD with free ship at https://www.aliexpress.com/item/32811843227.html I use AliExpress more than Ebay now since they finally take Paypal, and shipping is more reasonable than Ebay.

 
Last edited:

bhanlon56

New Member
@erco
I use aliexpress also.
I recycling 4.5V motors that I have. If I was starting from scratch I'd buy the robot chassis you get from aliexpress. I'm using same pins, c.0,c.1,c.2 and c.4. But I have srf-005 ultrasonic sensors that I'd love to use but they need an i/o. This post has more detail on what I'm doing. There's post by you somewhere here that details your use of radio transmitters. It hadn't occurred to me to use ir. Which technology would you use?
 

erco

Senior Member
Those srf-005 ultrasonic sensors can be modded for one-pin operation to use with one I/O pin per another thread here, but I'm not sure that would help here since you only have inputs pin left. I don't think my IR over RF trick brings anything new to your table.

There is another trick to use C.3 as an output by turning on & off its weak internal pullup resistor to control an external mosfet, but again that may not help here. Some pin multiplexing skullduggery/sorcery may be required...!
 

AllyCat

Senior Member
Hi,
I have srf-005 ultrasonic sensors that I'd love to use but they need an i/o. ...... use of radio transmitters. It hadn't occurred to me to use ir. Which technology would you use?
For a robot car you probably "should" use Infra-Red Remote Control, because the RF modules generally all use the same radio frequency and are intended only for "intermittent" (time-shared) operation (door bells, car/garage doorlocks, Weather sensor data, etc.). Also the RFOUT / RFIN commands are not supported by 08M2 chips; an off-the-shelf Universal and/or scrap (Sony TV) RC might give a reliable controller with little effort. However, interfacing the IR commands to a Radio Transmitter-Receiver pair is an ingenious compromise, that may easily permit multiple users within a single room.
___________

Of course if more than 3 or 4 "pins" are required then it's "sensible" to use a larger PICaxe (than 08M2); the 14M2 has twice as many I/O pins, much more memory (RAM, Table and Program slots, etc.), lower voltage operation and the RF commands, etc., for modest extra cost. However, the 08M2 is sufficient for many applications (particularly if using the I2C bus, or in an Educational environment) and may be all that's available to hand or from some third-party suppliers (e.g. CPC in UK). Also the 08M2 actually can be faster, simpler and more flexible (e.g. greater interrupt capability), because all its pins fit into the same port (which the PE conveniently allows to be declared as a single digit, or as Port B. or Port C.). Therefore, a user might have only some 08M2s, but all is not lost because the 08M2 has 6 pins which can ALL be used as both inputs and/or outputs, within certain limitations. The trick is to plan ahead and use the more "restricted" pins for the simpler tasks, so here's my summary of what CAN be done if you're desperate: ;)

Pin C.2 is generally the most "useful" pin because it has PWM Output capability, an (unofficial) Comparator Output, all the usual I/O functions, and most significantly it is the I2C Bus Data line (SDA). Similarly, the I2C SCL Clock line is a prime use for Pin C.1 , but it is also the DAC "Reference" voltage input, effectively a "Programmable Potentiometer", or simply an internal weak Pull-Down resistor. Officially, Pin C.0 is "Output Only" and is required for Programming and Debugging, but it CAN be used as an input and/or a "DAC" Output if required (although PWM via a low-pass filter generally makes a far better DAC). Then C.4 is another pin, with full Digital I/O and Analogue input capabilities, etc..

Officially, Pin C.3 is "Input Only", but it CAN be used as a limited Output by activating its Weak Pullup Resistor, as is available on ALL the 08M2's port pins. The current is restricted (and only from the supply rail) but is sufficient to drive the "Enable" pin of most chips (a high-value pulldown resistance may be needed), or an NPN transistor, or a MOSFET such as 2N7000. Similarly, Pin C.5 can be used for Input or Output with some care: First, a DISCONNECT command is required to prevent a Reset (initiating a new download), and in most cases the 10k pulldown resistor is best increased to 100k+ to increase the output voltage swing.

It's often commented that "The PICaxe Manual" has insufficient details on some topics, but there are many different PICaxes, so the most definitive information often can be obtained by using the Program Editor for a Syntax Check, or by Simulating a Test Program. This will often indicate which pins are available for specific functions such as PWM outputs or ADC inputs, etc.. However, sometimes it's "over-protective", requiring a workaround, for example: variable = %100000 : PULLUP variable , or a POKESFR command. For additional Hardware facilities, such as the Comparator, Timer1 Gate, Alternate Pin Functions and Interrupt on Change Flags, etc., the "Pin Allocation Table" (1), near the start of the related Microchip Data Sheet, is a good starting point to see most of the facilities that are available.

Finally, there are various circuit techniques that might be adopted, for example multi-level (Analogue) signalling, or Multiplexing, for which "Charlieplexing" is a popular method for driving more LEDs than there are pins available. ;)

Cheers, Alan.
 
Top