Scanner is driving me inSANE

Post Reply
User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Scanner is driving me inSANE

Post by mad_ady »

Wanted to work on a little "school" project - move an Epson Perfection V19 scanner to an Odroid server in my home and convert it into a network scanner that writes the scans to a network share so both kids can use it from a central location.
It turns out it's more complicated than I expected... Pardon my rant...

The main issue I expected to be with the buttons on the front panel, which are not supported in linux. But there are some projects that attempt to add button support: https://wiki.archlinux.org/title/Scanner_Button_Daemon, so I thought I'd look into them. But first, I want to be able to scan from the command line, thinking that I can trigger the scan from other external sources in the meantime.

There are some drivers on the epson site, and also sources of the drivers (that compile correctly!) here: http://support.epson.net/linux/en/epsonscan2.php
The original plan was to hook up the scanner to a N2 that runs Android and has a linux chroot. I installed the drivers, but the problem was that udev wasn't running and wasn't initializing the scanner correctly.

So I skipped on the Android idea for now. Better make it work on plain linux first.
I tried with a C1+, running Ubuntu 20. Compiled the drivers, but it complains about IO error when trying to run scanimage to actually scan. So I installed the deb variant of their drivers and now - it almost works. I am able to query the scanner (scanimage -A) and almost managed to scan (scanimage --format png -o test1.png), but it crashes the system with a "Stack smashing detected". I managed to scan only once, but got greedy and tried a second time, but the system crashed and lost my first scan.

Thinking that C1's USB ports are flimsy, I moved to a HC4 (running ubuntu 20 with kernel 5.11). Installed armhf as a foreign architecture and the 32 bit version of sane: apt-get install sane:armhf sane-utils:armhf libsane:armhf libgimp2.0:armhf libgegl-0.4-0:armhf libsdl2-2.0-0:armhf libasound2:armhf libasound2-data:armhf, plus the binary drivers. The scanner is detected (scanimage -A), but the USB port resets when trying to do a scan:

Code: Select all

65324.281454] usb usb1-port2: Cannot enable. Maybe the USB cable is bad?
[65325.181435] usb usb1-port2: Cannot enable. Maybe the USB cable is bad?
[65325.182544] usb usb1-port2: attempt power cycle
[65326.249394] usb 1-2: new high-speed USB device number 40 using xhci-hcd
[65326.279148] usb 1-2: New USB device found, idVendor=04b8, idProduct=013c, bcdDevice= 1.00
[65326.279163] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[65326.279169] usb 1-2: Manufacturer: EPSON
[65334.440357] TCP: request_sock_TCP: Possible SYN flooding on port 43975. Sending cookies.  Check SNMP counters.
[65336.875371] usb 1-2: USB disconnect, device number 40
[65337.181114] usb 1-2: new full-speed USB device number 41 using xhci-hcd
[65337.309135] usb 1-2: device descriptor read/64, error -71
[65337.545124] usb 1-2: device descriptor read/64, error -71
[65337.989098] usb 1-2: new full-speed USB device number 42 using xhci-hcd
[65338.117116] usb 1-2: device descriptor read/64, error -71
[65338.353103] usb 1-2: device descriptor read/64, error -71
[65338.461210] usb usb1-port2: attempt power cycle
[65339.113066] usb 1-2: new full-speed USB device number 43 using xhci-hcd
[65339.113150] usb 1-2: Device not responding to setup address.
[65339.321130] usb 1-2: Device not responding to setup address.
[65339.529057] usb 1-2: device not accepting address 43, error -71
[65339.657049] usb 1-2: new full-speed USB device number 44 using xhci-hcd
[65339.657130] usb 1-2: Device not responding to setup address.
[65339.865140] usb 1-2: Device not responding to setup address.
So, currently I'm stuck... Not sure how I can use this scanner with an Odroid (other than the H2, where it was already connected).
Anyone went on a scanning adventure on Odroids?

fvolk
Posts: 636
Joined: Sun Jun 05, 2016 11:04 pm
languages_spoken: english
ODROIDs: C2, C4, H2
Has thanked: 0
Been thanked: 77 times
Contact:

Re: Scanner is driving me inSANE

Post by fvolk »

Maybe just an anecdotal story, I have a scanner that works only on real USB 2 ports but not on USB 3 ports, although USB 3 should be backward compatibility?
Don't ask how long it took me to figure this out...

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

I've read about it. Might be related to the fact that it's using libusb to talk to the scanner.

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Well, did you check your USB cable? ;)
Because I used to struggle with Novation Launchpad that sometimes worked, even had some "lucky" machine and port combination that worked more often, only to realize that the cable was busted.

Anyway, separating a device from a machine with self-powered USB2 hub surely won't hurt, if the device for any reason draws more current from the host than it's supposed to.

Another root of evil are ground loops.
USB is a standard that causes a lot of headache for EEs. Contrary to Ethernet, that is always transformer isolated on both ends and differential in the name of the laws of physics, there are hardly ever any problems with connectivity, as long as standards are respected.
With USB, well.. Sometimes it takes connecting two devices that are plugged to different wall sockets to ruin the whole experience. Just as audio gear is well known to misbehave if you create a ground loop, USB is no different.
It's easy to spot a pattern that self-powered USB devices cause less problems in that regard.

But that's just something that is worth noting, especially if both devices connected with USB have class I power adapters (that also use protective earth connection). Or even worse, use the same power supply (just like my SATA HDD and N2+).

We actually fried a few laptop USB ports when connecting USB devices in similar circumstances at work. USB isolators are expensive and not very popular, sadly. In development environment we resorted to unplugging the laptop's AC power before connecting USB debugger to it.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

@mctom it's not the cable, but you are close enough. I tried yesterday with the OTG port on my C1, knowing that the regular usb ports are plagued with instability on the C1. And... it's a miracle! I was able to scan 6 pages!

But, of course, there's a catch... I need to unplug and replug the scanner after each scanned page, otherwise it errors out. So I'm working on automating that.

I expect it needs to be production-ready by the end of the week, since we're looking at several months of remote schooling once more. :(

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

I wonder what is the actual cause deep down in the roots. Is it the fault on software level, or perhaps scanner isn't exactly compliant with electrical standards.
If replugging the cable works for one page, it suggests that resetting driver state helps, as it doesn't change much from electronic point of view.
On the other hand, the driver is probably the same as on regular USB ports, which would indicate purely hardware issue.

Have you tried with a USB hub in between? :)

And that's a bummer your kids won't go to school again. Here the policy is much different - students go back to schools, and even get vaccinated there.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

Ok, I'm making progress! I was able to use the scanner on the C1's OTG port and ran it through a stress test. I made it scan the same thing in a loop and save it on a network share and afterwards reset the USB-OTG port and do it again. It can do a page every 50s at 100 DPI, which is not great, but not terrible... The great news is - I left it running for 30 minutes and it hasn't locked up or failed! Which is awesome!

Now, I only need to be able to use one of its buttons to initiate the scanning script. I'll look a bit at existing projects, but I have a sneaky idea... If I were to void the warranty and open this puppy up, I could probably solder a dupont cable to a pin of a button and have it connected to a GPIO on the C1. That way I could use interrupts or polling to see when the button was pressed and... presto!
I'll keep you guys updated.
In the meantime, here is my scanning script:

Code: Select all

#!/bin/bash


OUTDIR=/media/aldebaran/scanner/
if [ -d "$1" ]; then
        OUTDIR=$1
fi

echo "Turning blue led on"

D=`date +%Y-%m-%d_%H-%M-%S`
file="$OUTDIR/scan-$D.png"
echo "Scanning to $file..."

scanimage --mode Color --resolution 100 -o "$file"

if [ -f "$file" ]; then
        size=`stat --printf="%s" "$file"`
        echo "Scanned $size bytes"
else
        echo "Error while scanning..."
fi

echo "Resetting the USB bus..."
/usr/sbin/uhubctl -a cycle -l 2

echo "Restoring blue led state"
I want to use the blue heartbeat led to keep it solid on while scanning (as if the noise from the scanner isn't indication enough), but I can't seem to toggle it from userspace (I can change function)... Any ideas?

Code: Select all

$ cat /sys/class/leds/blue:heartbeat/trigger
[none] nand-disk heartbeat cpu0 cpu1 cpu2 cpu3 emmc sd 

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

Ok, I've decided to run some extra tests. Reconnected my scanner to my HC4, but this time using its original USB cable. What do you know? No more USB resets!
I've installed the armhf packages from epson's site, and also I've installed the armhf version of sane - specifically these packages:

Code: Select all

# apt-get install sane:armhf sane-utils:armhf libsane:armhf libgimp2.0:armhf libgegl-0.4-0:armhf libsdl2-2.0-0:armhf libasound2:armhf libasound2-data:armhf
I reran my 100 dpi scan to share test and I can do about 2.5 or 3 pages per minute! Much better.
Best yet, I don't need to reset the USB port anymore. So, it's future-proof and should work on other 64-bit Odroids as well (tested with USB2 only ports).

I'll look into using the buttons in some way...

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

So at the end of the day, was that the cable issue, or did I miss something?
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

InsideOut
Posts: 14
Joined: Sun May 13, 2018 4:02 pm
languages_spoken: english
ODROIDs: HC2
Has thanked: 4 times
Been thanked: 1 time
Contact:

Re: Scanner is driving me inSANE

Post by InsideOut »

I gave up trying to use saned with an Epson photo scanner attached to an HC2, I can't remember what the problem was. I switched to running the free version of Virtualhere's remote USB daemon on the HC2 with Epson desktop scanning software on the clients.

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

@mctom yes, the cable made a difference on the HC4. I'll retest on the C1, with the regular ports, but I expect it to work better with yhe OTG port. I think the driver (or usb stack) on the c1 may have some issues and may cause me to need to reset the port anyway.
@InsideOut - that's an interesting idea, thanks. But my goal is to enable scanning just by pressing a hardware button, so the kids can do it themselves, without the need to go back and forth to use the computer. What model of epson scanner do you have?

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

@mctom - I may need some soldering advice. I want to plug a wire from a button on the scanner to a GPIO on the C1.

I opened up the button panel of my scanner and shot some pictures and did some voltage measurements. Here are the pictures: https://imgur.com/a/K5VRbry

The bad news is the PCB is multilayered and the buttons seem to be SMD components and I can't easily solder a wire to a "leg". But I've traced the traces and I've identified the pins in the connector where the push buttons go. With the button not pressed, the pin is at 3.28V. When the button is pressed, the pin goes down to 0V. This is great.
I'm thinking I can get a wire from that pin to a C1 and read it as a digital input.
Here are some questions:
1. I don't think I have to worry about the ground potential, since the scanner is USB powered from the same C1 and both of them will be sharing the same ground, right? My measurements were against the ground I found on the connector.
2. Do I need to add any resistor to the circuit? I'm guessing that since the C1's GPIO will be configured as an input, there will be high impedance, which shouldn't affect anything in the scanner circuit, right?
3. Soldering will be tricky with my soldering skills. I doubt I can solder anything on the back of the PCB, since it needs to sit flush with the case, so I was thinking of soldering on one of the vias or test points I identified in the front. Do you think that would be ok? Anything I should watch out for?

Thanks

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

The PCB is so empty I'm 99% sure it's double layered - no hidden layers inside. In the industry "multilayered" refers to 3+ layers.

Yes, soldering to the buttons directly may impact their mechanical performance and it's a better idea to wire into something else.

There are resistors right next to each button - I think these are pullup resistors to 3.3V, and conveniently there are vias right next to them, including the one on the last photo (SW1).
Normally you see 3.3V coming through these resistors, but with a button pressed, this place is shorted to ground (with much smaller resistance), which you observe as 0V.
That means you could use these vias to solder wires thru-hole style, or wires to appropriate side of the resistor. Of course, check if these vias / resistors are actually what we think they are (which I think you did, just making sure we're on the same page).

If C1 is compatible with 3.3V inputs, it should be fine, but keep in mind the following problems:
- the ground connection provided by USB is theoretically OK for your purposes, but if the USB cable gets disconnected, it may be a fatal blow for C1.
- You really shouldn't provide extra ground path either, as you will create a ground loop, which is also to be avoided.
- You will pass signals through a rather long wire, which may pick up a lot of noise and cause false detections on C1 side.

If you connect the wire directly to C1, it will work most certainly. As a bonus, with such direct connection you may even use the same GPIO pin as output and simulate button presses back to a scanner. :)
But for safety measures, I'd use a resistor in series. Since GPIO inputs are high impedance in orders of Megaohms, adding anything between 1k - 100k shouldn't make a difference, but vastly limit the current in case of any failure.

So, wrapping up, I think 10k resistors in series for each C1 input is a reasonable protection.

If you happen to register false positives, you could add extra capacitor to form a first order passive low-pass filter.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Also: Yes, this connection should not affect normal operation of a scanner button in any way.
Soldering a wire to a test point is also fine, but be gentle, it's easier to tear it off the board, than via.
I wouldn-t advise soldering into the connector pins, though. Too much of a risk of melting the plastic and break it if not done quickly enough.

If the scanner is really USB powered and no external power supply is used, there risk is much smaller and probably you could try a direct connection without a series resistor.

Oh, and one more thing - don't forget about software debouncing. You may expect that a single button push will trigger C1 pin multiple times.
Switch_Debouncing_Circuit_Waveform.jpg
Switch_Debouncing_Circuit_Waveform.jpg (57.03 KiB) Viewed 170 times
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

L67GS
Posts: 600
Joined: Wed Apr 22, 2020 3:02 pm
languages_spoken: English, Jibberish, Pig Latin
ODROIDs: XU4, C1+,(3) C0's, and a whole big pile of accessories, VU7A Plus,, ect....
Location: Great Lakes Region, U.S.A
Has thanked: 163 times
Been thanked: 83 times
Contact:

Re: Scanner is driving me inSANE

Post by L67GS »

Ady, if you get creative with a few inches of buss wire and wrap it around the soldering iron tip it can be left out as a very fine tip. Once it's tinned to the tip it works quite nice for smd work. Hope this helps. The picture isn't clear enough to see the 2 pin decices next to each switch but they look like diodes or resistors in series with the switches, you may be able to go right there.

InsideOut
Posts: 14
Joined: Sun May 13, 2018 4:02 pm
languages_spoken: english
ODROIDs: HC2
Has thanked: 4 times
Been thanked: 1 time
Contact:

Re: Scanner is driving me inSANE

Post by InsideOut »

mad_ady wrote:
Sat Oct 02, 2021 2:16 pm
@mctom yes, the cable made a difference on the HC4. I'll retest on the C1, with the regular ports, but I expect it to work better with yhe OTG port. I think the driver (or usb stack) on the c1 may have some issues and may cause me to need to reset the port anyway.
@InsideOut - that's an interesting idea, thanks. But my goal is to enable scanning just by pressing a hardware button, so the kids can do it themselves, without the need to go back and forth to use the computer. What model of epson scanner do you have?
An Epson Perfection V300 Photo

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

Ok, thanks for your input, guys!
I managed to do the electrical connection via a 10k resistor, as advised. It seems to work just fine:

Code: Select all

 root  ~  #  gpio readall | grep 88
 |  88 |   0 |  IO. 88 |   IN | 0 | 11 || 12 | 0 | IN   | IO. 87  | 1   | 87  |
 root  ~  #  gpio readall | grep 88
 |  88 |   0 |  IO. 88 |   IN | 1 | 11 || 12 | 0 | IN   | IO. 87  | 1   | 87  |
I have some follow up questions:
- the ground connection provided by USB is theoretically OK for your purposes, but if the USB cable gets disconnected, it may be a fatal blow for C1.
Why is this? The pin should become floating and should behave the same way as if it were disconnected, no? Can you explain what would happen? Or is it the fact that there is a transitory event which might cause problems?
- You really shouldn't provide extra ground path either, as you will create a ground loop, which is also to be avoided.
I'm curious - what's the problem with ground loops? Is it that because of cable length, there will be a potential difference between two grounds?
- You really shouldn't provide extra ground path either, as you will create a ground loop, which is also to be avoided.
Yes, I expect this will happen. I will handle debouncing on the software side (no where to cram an extra capacitor).
As a bonus, with such direct connection you may even use the same GPIO pin as output and simulate button presses back to a scanner.
This would be nice. Too bad pressing the button does nothing without the appropriate driver... Which is why I'm going through this trouble in the first place. But, noted, might be useful in the future!
Ady, if you get creative with a few inches of buss wire and wrap it around the soldering iron tip it can be left out as a very fine tip. Once it's tinned to the tip it works quite nice for smd work
Thanks for the tip! But, without extensive practice, I think my soldering skills will remain on the noob-side... :D

I'll post the finished project details in the following days

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Wow, someone is actually interested in understanding hardware topics. That's new. :)

1. Why losing ground link between connected devices may be dangerous?
That matter turned out to be really complicated and hard to summarize reasonably shortly.
It depends on many factors, but let's narrow down the cases to two devices powered with independent isolated power supplies.
First of all, there are no perfect isolators. Isolated power supplies may still conduct via impedance in Mohm range. Totally safe to humans though. There are also a few design choices to choose from, for example some consumer power supplies connect output ground to wall socket earth pin. Many laptop chargers do that, for example.
Secondly, C1 GPIO input is high impedance indeed, but only for voltages in permitted 0V - 3.3V range. If you apply voltage from outside of that range (or any current!), input impedance is almost zero. This is actually an intended behavior of almost all chips since 1970s, designed to protect against ESD. This is done by placing two diodes on each pin, that conduct voltages lower than 0V to GND, and higher than 3.3V to internal 3.3V rail.

Given all that, if you wire two devices together using their grounds, some small current may flow through this wire, originating from not-so-perfect power supplies. Unless the resistance of the connection is small enough, two devices may be considered as having a single, common ground and nothing bad happens. Ground nets in all devices have the strongest current sinking capabilities.
Now if you remove ground connection and put only signal connection instead, that parasitic current may flow through that. Not so great, given that input pins are much more fragile.
So to summarize, you get a random current flow through a wire that ends on a high impedance GPIO input. Small random current times high impedance may yield random voltage on your input pin. Which is not bad, you'll just get nonsense input data. It is very common to get 50Hz noise this way.
In a fatal case, with exceptionally s#!t power supplies and communist era wiring in the walls (Neutral pin == ground pin), the leakage current may get higher than the capability of ESD protection diodes on C1 side.
That's the reason why I suggested 10k resistor - to limit parasitic current, if ground connection fails.

I mentioned sometime ago that exactly this reason killed many USB ports and USB debuggers in my workplace. The workaround was to unplug laptop chargers for the time of work. It is also not uncommon to have at least one battery-powered oscilloscope in the lab.

2. Ground loops are bad

For DC and low frequency signals, ground is something that you take for granted - it is connected, so it must work. With higher frequencies, strange effects come into play: it takes more current to switch voltage back and forth, and as we know, current always flows in loops. Current return paths become significant for signal integrity, and this is what ground is usually used for. For really damn high frequencies, laws of physics tend to select a current return path that is counter intuitive - not the shortest one, not the one with the lowest resistance, but the one with the lowest impedance - which happens to be right in parallel to the original signal. For this very reason fast signals are always routed against ground, or even have their own dedicated return paths - which is essentially differential signalling.
So what if the return path gets disrupted for any reason? High frequency signals may partially be attenuated, reflected back and/or turned into a radio wave. In the worst case the receiver won't understand signal anymore, and in a better case your device won't pass electromagnetic compatibility certification.
This is just an example of a universal rule everywhere where voltage changes in time. Ground is not a lump of metal with constant 0V across it, if you consider any non-DC signals.
For this reasons, if ground forms a loop (which is always strictly avoided when designing anything), it forms quite good antenna. Not only current flowing through this loop will happily radiate, it will also absorb radio background we live in, and trust me it's terrible since consumers want f**ng everything wireless these days.

Another, totally unrelated reason why ground loops are bad: because they provide unintended current paths. In your example, if you added parallel ground path from C1 to a scanner, but ending somewhere on PCB with buttons, it would cause some current to go through the original path, and some through a new one. All sorts of things may go wrong from this point onward: a ground connection from button PCB to main PCB may be too weak and fail, for example. It may introduce noise to analog scanning circuitry. Basically, nothing good may ever come out of that.

Ground loop problems are so serious it's a common practice to select a "star point" in every design process - where all grounds eventually meet without looping, only branching is permitted. Even the whole spacecraft has a single star point.

I created a ground loop in my apartment some 10 years ago. I had a PC, its ground was connected to earth pin. I had a video projector on the other corner of the room, its ground also connected to earth pin. Then I used 15m VGA cable to connect them both. The ground loop was almost 30m long and had a few square meters of area. Picture was so bad it was not possible to navigate GUI. I solved the problem completely by removing earth pin connection from the projector (Please don't do that).
I can't tell which effect was stronger in this ground loop case. Either a huge antenna captured all sorts of space radiation garbage and presented it on my screen. Or, the fact that I provided an alternative path from one wall socket to the other, and added that noise to my RGB signals.
That is the reason why most audio gear has Class II power supplies - you can tell because their plugs don't make connection to earth pin. Analog signals are of course far more susceptible to ground loop problems.
The same reason why Ethernet is always isolated on every end of a cable. Imagine what would happen if it wasn't! :D

3. Debouncing

First time I heard about the problem was at the lecture, of course. I asked professor why won't we just add a capacitor to filter it out? I'm really glad I asked.
- It takes space and costs money
- It reduces rate of voltage change, and if it gets too slow, digital inputs without hysteresis ("non-Schmitt") may register digital noise on their own,
- Low pass filters introduce delay, and that might be unacceptable in certain cases, such as musical instruments.

So, yeah, do it in software. ;)

4. Driving external stuff

I have an audio amp connected to 24V, and my stack of SBCs running off 5V. I wanted to automatically turn off audio amp using a dedicated pin on the board. So I did the direct connection and it worked, but added constant electronic buzz to my speakers - I could have heard RasPi's power supply noise all the time.
This is a prime example how ground loop is causing trouble not by picking up external noise, but by introducing unintentional current paths where not appropriate.
The proper way to achieve the same goal is what prompted me to design one feature of PiStackMon. It has four optically isolated outputs, that can short something together or not, without any galvanic connection. I use this feature to pull "enable" pin low on audio amp and it works great. :)
So next time grab PiStackMon for controlling external stuff. :roll:
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

Thanks for yhe detailed explanation! I love learning new stuff. That's why I'm here :D

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Yeah, me too. I used to think Mensa membership would make me learn stuff, but it was more of an alcoholic swingers club really. FOSS community is far better in this regard.
Looking forward for your results. :)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

Ok, first iteration is done. Uploaded the code that handles button scanning and debouncing (python), the actual scanning script (bash) and a systemd service to start it on boot: https://github.com/mad-ady/scanner-button-from-gpio
Documentation is minimal, like my spare time :D

However, for my next trick - now that I know it works under linux, I'll try to make it work under Android, as I initially intended :D Wish me luck!

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

So your debouncing code is basically a (kind of) low pass filter (1st step) and hysteresis (2nd step). :)
I'm worried this will work awkwardly, it will require the user to push a button for minimum time of 0.3s - it may be too long. Compare with your typing speed on a keyboard. More than 3Hz, I believe. :)

Typical debouncing code (that I saw in the industry) involves detecting button press as usual, but ignoring any additional button presses for 50ms (or 1s would be just as good in your case).
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

What can I say? 0.3s shows user commitment, and not just a simple press. It also makes the loop run less frequently, lowering cpu usage (<1%). Less frequent polling = less chances of random noise triggering scanning in the middle of the night...

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Fair point. :)

Also, a minor concern: if MAXIMUM is not an integer, then after button release the 'integrator' variable will jump below zero. Nothing critical though. :)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mad_ady
Posts: 9783
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 613 times
Been thanked: 734 times
Contact:

Re: Scanner is driving me inSANE

Post by mad_ady »

An update from the home schooling front. My Android tests failed. The 32-bit drivers were installed correctly under 64-bit linux rootfs under Android (with the 32-bit scanimage package and dependencies). I can query the scanner parameters and it initializes (motors whir), but it fails when doing the actual scan (software error - behaves like it has a bad cable). I tried on the USB-OTG port, with the scanner's original cable.

Oh, well, I'll have to keep the C1 around in production.
Now - some extra findings. C1, with a short power cable (from the original Go Advanced) and a 1A phone charger works ok with the scanner (though the PSU is under C1's 2A specs). The same 1A PSU with a longer power cable (that I built from a long USB data cable and a spare connector) works, but creates scanning artifacts (red lines) in the scanned image, like noise. Most likely it's a cause of under-powering the USB port. I remember the USB cable I used had thin power wires (not rated for charging or such nonsense), so I expect it has high resistance at higher currents. The same cable with a 2A charger creates lines as well, but fewer.

Now - I've let the kids use the scanner in production, and got feedback from the end users. It seems I may need to wire up the extra buttons as well. There's a demand for scanning a bunch of pages into a PDF document and scanning and rotating pages. For PDF scanning (and appending multiple pages to a document) I will add a button as a toggle. When you press it, the script will remember to append all following pages to a new document, until you press it again. Sadly, it's not user-friendly, so I was thinking of adding voice prompts that tell you what will happen when you press a button - things like "Scanning to new PDF document", "Scanning to files", "Scanning portrait page", "Scanning landscape page". The quirk is - the C1 doesn't have speakers attached, so I need to push the voice files over network to the N2 next to it and have it played back via MPD.

Yes, it looks like I'm building a one-of-a-kind Rube-Goldberg system here... I'll keep you posted... I'm having loads more fun doing this than buying a multi-functional that can scan to network share...

User avatar
mctom
Posts: 518
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 50 times
Been thanked: 49 times
Contact:

Re: Scanner is driving me inSANE

Post by mctom »

Jeez :D I'd drill a hole next to a pdf button and stuff LED in there. And make it pink! ;)

USB cables are the worst, mostly because the market is flooded with low quality cables.
USB chargers suck as well. ;)

Indeed, one problem is resistance and consequently voltage drop on the wires. Voltage drop is a product of resistance and current. In my stack, I drew about 10A and got about 1V drop, which means 0,1Ohm of total resistance of wires and connectors. Note that resistance of both "plus" and "minus" paths add up.

I think your C1 worked okay, because it doesn't need 5V to work correctly. Internally there is a bunch of Buck converters, with the highest voltage of 3.3V, which will do fine with input as low as 3.6V or so. I do this all the time with RasPis.

5V input is necessary only for USB devices, and that's where you see problems. USB standard assumes power line voltage within 5% of nominal 5V, so should work down to 4.75V.
C1 current specs are a gamble, I'd say - USB standard foresees 500mA for each USB port, so the whole 2A budget could theoretically be consumed by USB devices. But that's really unlikely. That's why you probably saw no major problems with 1A PSU.

Current rating of a PSU is a maximum guaranteed output current, and the true maximum should be somewhere higher.
In reality most cheap PSUs cannot deliver nominal current, because consumers can't measure that, and phone/tablet charging circuits adapt to whatever power source they get.
USB chargers are also notoriously bad at maintaining constant output voltage when load varies. Simply speaking, when current demand suddenly rises, those are too slow to act on it, so a transient voltage dip occurs. This is an unlikely scenario while charging a battery.
That may be a cause of some of your problems. If that is the case, a bulk capacitor on the power supply input of C1 would surely help.

In theory you could do even better and add bulk capacitors where the most severe voltage swings occur - to 5V rail inside a scanner. But that's not the best idea - yet again, USB standard limits the capacitance seen by the host to 10uF. Ignoring these requirements may cause C1's fuse to blow by initial capacitor charge-up.

Although tinkering is fine, I'd rather suggest getting quality USB cables instead. Both of them. :)
These users thanked the author mctom for the post:
mad_ady (Mon Oct 11, 2021 5:01 pm)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

Post Reply

Return to “General Topics”

Who is online

Users browsing this forum: No registered users and 2 guests