Fastest possible ADC response?

rq3

Senior Member
First, Happy New Year to all, and I trust everyone is staying sane and in good health!

I have a 3D printer, which uses a microswitch to physically probe the printer bed location. The microswitch gets fed +5 volts (approximately, it's actually about 4.2 volts) through a 4.7K pullup from the printer microprocessor board. The switch is normally closed, so the microprocessor sees logic low. When the switch contacts the printer bed, it opens, and allows the pullup to pull the signal high (+5 v), telling the microprocessor that contact has been made.

The printer control board is closed source, so I have no firm data on which ports it is using, and how the port and pullup is physically configured.

I have been playing with a piezoelectric disc and a Picaxe 20M2. The intent is that when the printer nozzle itself contacts the printer bed, a signal is generated. This is an old idea, and there are actually commercial products that do this, so as to negate the need for an actual physical switch, and the knowledge of the switch's physical offset from the nozzle.

My question is, what is the fastest possible way to have a Picaxe pin go from active low (switch closed) to "open" (or a high impedance state, NOT active high), based on an ADC input (or maybe comparator input, or even logic input) on another pin? I've done the obvious (setfreq M32, etc.), and don't want to have the pin go active high, as the Picaxe is on its own 5 volt supply, and I don't want to risk "back driving" the printer's logic line.

Ideally, I'd do this with an 08M2, rather than the 20M2's I have on hand, so any tricks to invoke a comparator function (if that would be faster than an ADC or logic input) would be very welcome. I'm looking at you, AllyCat ;-) but welcome any and all thoughts!
 
Last edited:

AllyCat

Senior Member
Hi,

It's late here, so just a few quick comments for now: First, I can't think of any "hardware" function that switches instantly from "Output" to "Tri-state" so you will need to use either an INPUT or REVERSE instruction, which typically takes 400 us @ 4MHz or 50 us @ 32MHz. DIRS probably takes a little longer.

READADC{10} is not particularly "slow" (IIRC < 1ms @ 4MHz) but I don't believe the actual speed of ADC conversion is changed by SETFREQ, so you might need to look at the "base PIC" data sheet and the ADC SFRs to optimise any higher speed. There is no direct (non-polled) interrupt so you will need a tight loop to check the ADC (or a pin, etc.) but a single IF <condition> THEN GOTO <itself> (else falls through) takes only just over 1ms @ 4MHz (perhaps 150 us @ 32 MHz).

Alternatively, even the 08M2 has one Comparator (the other M2s have two) "in silicon", which isn't difficult to use with a few POKESFR commands. I may have written something on using them, but I believe so has @kranenborg. Also, you can use the internal "DAC" (which is supported by PICaxe Basic commands) to bias one input of the comparator(s) and then read its output with a PEEKSFR (thus no pins are required except for the analogue input). Or feed the Comparator output to its pin (c.2 with an 08M2) and then into another pin to generate an interrupt, or read that pin (you can't read the output pin internally, because it will disable the output mode).

Indicate which approach looks more "interesting" and maybe I can expand on it in the morning. ;)

Cheers, Alan.
 

J G

Active member
I haven't ever looked at the comparators or DAC, but for going from active low to high impdence state, could you use an N channel FET or NPN transistor to pull the signal wire to ground and then switch it off to go open circuit (up to the breakdown voltage of the component used)? This would be controlled by a high and low signal from an output pin of the picaxe. This would also protect the chip should the printer do something weird like presenting a higher voltage or voltage spikes >5V on the wire.

EDIT: If nothing really clever / requiring programming is required, could you use a comparator or op amp to condition the signal from the piezo disk and directly drive the transistor with little delay and not use a microcontroller at all?
 
Last edited:

rq3

Senior Member
I haven't ever looked at the comparators or DAC, but for going from active low to high impdence state, could you use an N channel FET or NPN transistor to pull the signal wire to ground and then switch it off to go open circuit (up to the breakdown voltage of the component used)? This would be controlled by a high and low signal from an output pin of the picaxe. This would also protect the chip should the printer do something weird like presenting a higher voltage or voltage spikes >5V on the wire.

EDIT: If nothing really clever / requiring programming is required, could you use a comparator or op amp to condition the signal from the piezo disk and directly drive the transistor with little delay and not use a microcontroller at all?
What? No picaxe? That's cheating! Anyway, I've breadboarded a primitive mock-up with a quad comparator, and also with a 20M2.
The picaxe is much more stable doing a simple ADC read. It can reliably detect a BB dropped on my bench about 4 feet from the piezo disc. The comparator needs a LOT of hysteresis for noise immunity.
 
  • Like
Reactions: J G

rq3

Senior Member
Hi,

It's late here, so just a few quick comments for now: First, I can't think of any "hardware" function that switches instantly from "Output" to "Tri-state" so you will need to use either an INPUT or REVERSE instruction, which typically takes 400 us @ 4MHz or 50 us @ 32MHz. DIRS probably takes a little longer.

READADC{10} is not particularly "slow" (IIRC < 1ms @ 4MHz) but I don't believe the actual speed of ADC conversion is changed by SETFREQ, so you might need to look at the "base PIC" data sheet and the ADC SFRs to optimise any higher speed. There is no direct (non-polled) interrupt so you will need a tight loop to check the ADC (or a pin, etc.) but a single IF <condition> THEN GOTO <itself> (else falls through) takes only just over 1ms @ 4MHz (perhaps 150 us @ 32 MHz).

Alternatively, even the 08M2 has one Comparator (the other M2s have two) "in silicon", which isn't difficult to use with a few POKESFR commands. I may have written something on using them, but I believe so has @kranenborg. Also, you can use the internal "DAC" (which is supported by PICaxe Basic commands) to bias one input of the comparator(s) and then read its output with a PEEKSFR (thus no pins are required except for the analogue input). Or feed the Comparator output to its pin (c.2 with an 08M2) and then into another pin to generate an interrupt, or read that pin (you can't read the output pin internally, because it will disable the output mode).

Indicate which approach looks more "interesting" and maybe I can expand on it in the morning. ;)

Cheers, Alan.
Thanks, Alan. I'll look into the 08M2 comparator SFR registers, unless you have some info ready at hand? Playing with the printer and a scope, this appears to be not really time sensitive, as they appear to have debounce for the microswitch baked into the firmware. I may actually be chasing an unnecessary wild goose.
 

oracacle

Senior Member
This is an interesting idea, however you still need to know offsets as the piezo disc had thickness and the nozzle should be less than 0.1mm from the bed.
Depending on what board your using it will be faster, simpler, easier and more accurate to use the BL or 3D touch, flash your firmware for it. You can also make use of auto bed leveling as well then.

I built a project that detects sounds and triggers a camera. It made use of a 28x2 running at 64mhz and a competitor interrupt. Iirc I measured from the ADC reaching the predetermined level to an output changing state was in the order of 150us
 

rq3

Senior Member
This is an interesting idea, however you still need to know offsets as the piezo disc had thickness and the nozzle should be less than 0.1mm from the bed.
Depending on what board your using it will be faster, simpler, easier and more accurate to use the BL or 3D touch, flash your firmware for it. You can also make use of auto bed leveling as well then.

I built a project that detects sounds and triggers a camera. It made use of a 28x2 running at 64mhz and a competitor interrupt. Iirc I measured from the ADC reaching the predetermined level to an output changing state was in the order of 150us
Hi oracacle. That 150 us, or even a millisecond, should be more than fast enough. I'm aware of the commercial products, but they seem awfully overkill and persnickety. My intent here is to glue the piezo disc (suitably modified) under the bed (it's a delta printer), and detect when the nozzle actually taps anywhere on the bed. That should get me a true zero, and complete bed leveling, without the need for Z axis offsets I currently have to compensate for when using the microswitch (which actually works very well). An M48 g-code repeatability test of the microswitch generally yields a standard deviation of about 3 microns.

What got my attention is that all of the existing piezo designs go to great pains to mount the disk so that it can be physically flexed, while clamped, which isn't how piezo discs are "normally" mounted. My experiments show that if the disc is heavily mass constrained at the rim and the center but free to flex, it will act like a seismograph, and respond to extremely small displacements (nanometers, picometers?) at the center (extremely small as in dropping a BB on the bench 4 feet away).

I have a heavy brass ring epoxied to the rim of the piezo disc, and a heavy brass "stem" glued to the center of the brass side of the disc, so the whole thing looks like a mushroom. With the stem sitting on the bench, it's wickedly sensitive, but needs a distinct "tap" (the BB) to respond because of all of the added mass. It doesn't seem to respond to slow inputs, or reasonable changes of temperature from a hot air gun. I have a primitive low pass filter and a 100K ohm shunt on the output of the disc.

Just playing!
 
Last edited:

kranenborg

Senior Member
...
Alternatively, even the 08M2 has one Comparator (the other M2s have two) "in silicon", which isn't difficult to use with a few POKESFR commands. I may have written something on using them, but I believe so has @kranenborg. Also, you can use the internal "DAC" (which is supported by PICaxe Basic commands) to bias one input of the comparator(s) and then read its output with a PEEKSFR (thus no pins are required except for the analogue input). Or feed the Comparator output to its pin (c.2 with an 08M2) and then into another pin to generate an interrupt, or read that pin (you can't read the output pin internally, because it will disable the output mode).
...

Cheers, Alan.
Hi, a bit late with my response (currently I am happily but thoroughly distracted by a non-Picaxe, analog project, using my youth's Philips EE (Electronic Engineer) system, see: Alternative Superhet (SA612A mixer & ZN414 detector) - Baukasten Forum / CC Experimentiersystem -> WIKI: /www.rigert.com/ee-wiki ) ... but the Picaxe is not out of my mind. By the way, the Philips EE system (production ceased in 1983) is still my prototyping system for all my electronics projects, including Picaxe.

Indeed I have written something on this subject a while ago, since the M2 chips contain hardware functionality like comparators that may not be directly supported by the BASIC command set (I presume that this is because the flash memory containing the interpreter for the M2 chips is half the size of the X2 chips, so not all BASIC features can likely be supported). But - as AllyCat already indicated - there are the POKESFR commands to help us out and use and connect the modules in that way without any limitation. Please have a look at the use of comparators in the first and third item of the following link: PICAXE and ZBASIC code contributions (kranenborg.org) . In particular the first one is interesting since it shows how the DAC, comparator and SR-Latch can be tied together to immediately catch and generate a digital signal from a fast, very short , analog pulse to the polled (so relatively slow) interrupt pin. This all happens in hardware, so no PICAXE firmware intervention needed, and the response time is limited only by the chip's hardware response times (and the Pixace Interrupt firmware of course to pick up this latched signal and act on it). The third - slightly older - item shows the configuration and use of the comparators themselves using POKESFR commands (including direct output to a pin, again without Picaxe program intervention). I wrote that contribution at a time when only the 18M2 existed from the M2 series (and I think it was even an early silicon version), but the concept will likely apply to all M2's - the 08M2 has one comparator, the other M2's have two.

Cheers,
Jurjen
 
Last edited:

AllyCat

Senior Member
Hi,
.... it shows how the DAC, comparator and SR-Latch can be tied together to immediately catch and generate a digital signal from a fast, very short , analog pulse to the polled (so relatively slow) interrupt pin.
Yes, I also showed a similar "trick" using the "Timer 1 Gate" hardware (not the Timer itself) in post #10 HERE to give a fast Latching or Toggle facility. However, both methods have been made partly "Obsolete" by hippy's revelation of the "Interrupt on Change" flags, accessible via POKE/PEEKSFR commands in all M2s. The "Interrupt" part of the name is misleading (for PICaxe applications) but the "Flags" (register) can be set to Latch on either positive, negative or both edges of a digital input signal, and then later polled by a PEEKSFR.

THE IOC flags are available on ALL 08M2 I/O pins (including the programming input and output), 6 pins on the 14M2, 8 on the 18M2 and 10 pins on the 20M2. However, the SFR port.pin names' mapping are rather "complicated", particularly on the 20M2. The IOC flags have been discussed by hippy and others from post #3 in THIS THREAD.

Cheers, Alan.
 

rq3

Senior Member
Hi, a bit late with my response (currently I am happily but thoroughly distracted by a non-Picaxe, analog project, using my youth's Philips EE (Electronic Engineer) system, see: Alternative Superhet (SA612A mixer & ZN414 detector) - Baukasten Forum / CC Experimentiersystem -> WIKI: /www.rigert.com/ee-wiki ) ... but the Picaxe is not out of my mind. By the way, the Philips EE system (production ceased in 1983) is still my prototyping system for all my electronics projects, including Picaxe.

Indeed I have written something on this subject a while ago, since the M2 chips contain hardware functionality like comparators that may not be directly supported by the BASIC command set (I presume that this is because the flash memory containing the interpreter for the M2 chips is half the size of the X2 chips, so not all BASIC features can likely be supported). But - as AllyCat already indicated - there are the POKESFR commands to help us out and use and connect the modules in that way without any limitation. Please have a look at the use of comparators in the first and third item of the following link: PICAXE and ZBASIC code contributions (kranenborg.org) . In particular the first one is interesting since it shows how the DAC, comparator and SR-Latch can be tied together to immediately catch and generate a digital signal from a fast, very short , analog pulse to the polled (so relatively slow) interrupt pin. This all happens in hardware, so no PICAXE firmware intervention needed, and the response time is limited only by the chip's hardware response times (and the Pixace Interrupt firmware of course to pick up this latched signal and act on it). The third - slightly older - item shows the configuration and use of the comparators themselves using POKESFR commands (including direct output to a pin, again without Picaxe program intervention). I wrote that contribution at a time when only the 18M2 existed from the M2 series (and I think it was even an early silicon version), but the concept will likely apply to all M2's - the 08M2 has one comparator, the other M2's have two.

Cheers,
Jurjen
Jurjen, very good stuff. I will play with this!
 

rq3

Senior Member
Hi,

Yes, I also showed a similar "trick" using the "Timer 1 Gate" hardware (not the Timer itself) in post #10 HERE to give a fast Latching or Toggle facility. However, both methods have been made partly "Obsolete" by hippy's revelation of the "Interrupt on Change" flags, accessible via POKE/PEEKSFR commands in all M2s. The "Interrupt" part of the name is misleading (for PICaxe applications) but the "Flags" (register) can be set to Latch on either positive, negative or both edges of a digital input signal, and then later polled by a PEEKSFR.

THE IOC flags are available on ALL 08M2 I/O pins (including the programming input and output), 6 pins on the 14M2, 8 on the 18M2 and 10 pins on the 20M2. However, the SFR port.pin names' mapping are rather "complicated", particularly on the 20M2. The IOC flags have been discussed by hippy and others from post #3 in THIS THREAD.

Cheers, Alan.
Alan, that is invaluable!
 

julianE

Senior Member
Hi, a bit late with my response (currently I am happily but thoroughly distracted by a non-Picaxe, analog project, using my youth's Philips EE (Electronic Engineer) system, see: Alternative Superhet (SA612A mixer & ZN414 detector) - Baukasten Forum / CC Experimentiersystem -> WIKI: /www.rigert.com/ee-wiki ) ...
I really like your superhet project. I was experimenting with the LM3900 Op-Amp earlier in the day. It's an odd duck op-amp i seldom see anyone use it. I'm curious why you chose it for your project. BTW while experimenting with the LM3900 as an audio preamp, by just touching one of the resistors and acting as an antenna I can perfectly hear a local middle wave radio station.
 

kranenborg

Senior Member
I really like your superhet project. I was experimenting with the LM3900 Op-Amp earlier in the day. It's an odd duck op-amp i seldom see anyone use it. I'm curious why you chose it for your project. BTW while experimenting with the LM3900 as an audio preamp, by just touching one of the resistors and acting as an antenna I can perfectly hear a local middle wave radio station.
(A little bit off-topic regarding the PICAXE) Thanks for your kind remarks! I chose to use the LM3900 simply for nostalgic reason, as this was the quad opamp chip provided with the EE2001 series of kits - it is mounted on the yellow circuit board. Of course, today one would use a modern amp, but it was the first quad-opamp available in the early seventies and since the EE2001 series was conceived in 1976 this implies that these kits were quite state-of-the-art. The Philips EE2000 and EE2001 series really defined my youth (each year I asked for an extension kit, and there were quite a few to choose from, as you can see here: http://ee.old.no ). I managed to collect new old-stock kits from Ebay during the last fifteen years and as a consequence I have a quite absurd but highly appreciated collection that I practically use, as shown in the picture included here. As you can see I also have a few of the older EE1000 predecessor kits that were sold extensively in the UK as well.

Philips EE Kast - small.jpg

BTW, This is the PICAXE-08M2 "seat" that I made for the Philips EE system:




Regards,
Jurjen
 

premelec

Senior Member
Spring into action!... so many old proto boards - used to be Fahnestock clips among other troublesome connectors... binding posts stacked banana plugs... what ever works - yay for empirical bread boarding - even dead bug style... and free form 3D soldered constructions that actually do something...
 

julianE

Senior Member
( I chose to use the LM3900 simply for nostalgic reason, as this was the quad opamp chip provided with the EE2001 series of kits - it is mounted on the yellow circuit board. O
I have a couple 20 year old LM3900s and the more i read about LM3900 the more interesting it is. Learning more details about Norton Op Amps and how they operate based on current as opposed to voltage.

BTW, your work area and display of all your kits is just perfect. I have to try to make my work bench 1/10th as nice, instead of a pile of parts and breadboards.

In USA we have snapcircuits an update to old classics. I bought a set for my nieces and it's rather good, i'm looking for ways to integrate PICAXE into the snapcircuits.
 

inglewoodpete

Senior Member
Spring into action!... so many old proto boards - used to be Fahnestock clips among other troublesome connectors... binding posts stacked banana plugs... what ever works - yay for empirical bread boarding - even dead bug style... and free form 3D soldered constructions that actually do something...
Fahnestock clips? You must be from a similar era to me! Santa can be credited with starting my hobby, which resulted in a career in electronics then later, software with a kit that used a board studded with Fahnestock clips. Just a few years later, younger kids had kits that used exposed spring terminals like Jurjen's.
 

rq3

Senior Member
This is an interesting idea, however you still need to know offsets as the piezo disc had thickness and the nozzle should be less than 0.1mm from the bed.
Depending on what board your using it will be faster, simpler, easier and more accurate to use the BL or 3D touch, flash your firmware for it. You can also make use of auto bed leveling as well then.

I built a project that detects sounds and triggers a camera. It made use of a 28x2 running at 64mhz and a competitor interrupt. Iirc I measured from the ADC reaching the predetermined level to an output changing state was in the order of 150us
Well, that was interesting. Attached are photos of the resulting piezo sensor, which is almost immune to stepper motor movement and acoustic noise, but VERY sensitive to quick taps. The steel ball glued to the piezo is crucial and I have the entire sensor screwed to the interior of the electronics cabinet of my delta printer, where it was easy to access +24 volts for supply, and connect the output to the printer controller board. The sensor does not have to be attached to the bed of the printer. It only has to be firmly mechanically coupled anywhere on the printer frame.

The firmware I use on the printer (Marlin) is not capable of reporting the standard deviation of a z axis (M48) probe test. It always reports 0.000000.
I have also successfully run delta calibrations and bed leveling, with excellent print results. The trickiest part of this was trying not to overthink the software, as the printer control board expects certain delays because it normally uses a mechanical microswitch for z axis probe detection. I went through various iterations of ADC configuration including different reference voltages, 8 bit and 10 bit, clock speeds, and averaging.

I ended up with this:

Code:
#REM
Piezo bed leveling sensor using Murata 41 mm piezo disc
with seismometer mass. Output acts like original Anycubic
Predator microswitch; actively pulled low when not touched,
and tri-stated when actuated to allow printer microprocessor
to pull output high via 4K7 ohm resistor on Trigorilla-Pro board.                            
RED is common ground, BLUE is LEVEL pull-up on Z- connector.
2/13/21 by RQ3
#ENDREM

#Picaxe 08M2
#no_data
#no_end

symbol LED=C.4
symbol OUT=C.2            
symbol PIEZO=C.1
symbol TRIGGER=8                 ;sensitivity 0-1023(max-min)

init:
setfreq M32                            ;run fast
pullup %01000                       ;apply pull-up to unused C.3
adcsetup=%00010                 ;make C.1 ADC input
pause 4000                             ;settling time

main:
    do
        low LED,OUT                  ;pull outputs active low
        readadc10 PIEZO,w0      ;read the readadc
        if w0>TRIGGER then      ;if there is piezo output
            input OUT                  ;tri-state output pin to allow external pull-up
            high LED                    ;turn on LED
            pause 8                      ;wait 1 msec to see LED, and allow signal capture
        endif
    loop
The trickiest part was getting the printer control board timing correct. And it was interesting that a 32MHz Picaxe clock worked the best. While delving into the details of the Picaxe ADC was fascinating, I hope someone can make use of the mechanical aspects of this idea. An acoustically damped piezoelectric tap sensor. Gramps dulcimer hammer project comes to mind.
 

Attachments

Last edited:

rq3

Senior Member
@julianE, I used J-B Weld, which is a steel loaded epoxy with a very high temperature rating. It's common in the U.S., but for this application I think any decent epoxy would be fine.
 
Top