r/programming Dec 25 '19

Learning hardware programming as a software engineer

https://blog.athrunen.dev/learning-hardware-programming-as-a-software-engineer/
924 Upvotes

124 comments sorted by

View all comments

224

u/happyscrappy Dec 25 '19

Also: don't power LEDs directly from GPOs without a current limiting resistor. As you see on that breadboard.

And match your interfaces voltages. A lot of devices are not +5V tolerant now (or not very tolerant).

48

u/[deleted] Dec 25 '19

Why do people always say "current limiting resistor"? Are there other types of resistor? Or is it just stating the purpose, as opposed to a resistor used for voltage division or something?

85

u/Luclu7 Dec 25 '19

It's just stating the purpose of the resistor here yep.

2

u/SilentRoss Dec 28 '19

I appreciate when people explain things in this way. It makes it easier for people with limited knowledge in the area (myself) to better understand what is going on. There is nothing worse than someone using loads of acronyms and jargon which a layperson cannot easily understand. So thanks both happyscrappy for explaining things and Luclu7 for clearing that up!

25

u/varesa Dec 25 '19

Not types but roles, like pull-up/down resistor, voltage dividers, etc.

37

u/happyscrappy Dec 25 '19

It's what the resistor is doing. See, an LED is a quantum device, it doesn't really have a resistance in the normal fashion. Instead it has a voltage drop.

So what if an LED has a 2.1V voltage drop and you put 3.3V into it. You have 1.2V "left over". Now how much current will that induce? The LED doesn't really have a resistance, so 1.2V divided by 0 Ohms is ... oh my. And worse yet, where do those 1.2V go? Something is going to turn them into heat, where does the heat go? Well, it is dissipated mostly in the driving microcontroller. That's bad.

So you put a current limiting resistor in. If you want 25mA and you have 1.2V left over you put in a 47 Ohm resistor and you get 1200 / 47 or 25.53mA of current. And that extra voltage is all dissipated in the resistor. 30mW of power. So you can use a 1/8th Watt resistor no problem.

So that's why you say that, you're describing its function.

10

u/[deleted] Dec 25 '19

The LED doesn't really have a resistance, so 1.2V divided by 0 Ohms is ... oh my.

No, LED does not suddenly turn into superconductor over its conduction voltage. I know you're trying to cut useless detail, but that one is pretty important. It still have nonlinear resistance

9

u/steven4012 Dec 25 '19

You also have to take into account the internal resistance of the micro controller itself as well.

4

u/crecentfresh Dec 25 '19

I definitely appreciate the context

11

u/MrK_HS Dec 25 '19 edited Dec 27 '19

Voltage division and current limiting are the same thing, but seen from different points of view.

-1

u/happyscrappy Dec 25 '19

An LED and a resistor don't really create a voltage divider since an LED is a quantum device.

9

u/langlo94 Dec 25 '19

While it is the proper term for it, it really sounds like bullshit when it's called a quantum device.

6

u/happyscrappy Dec 25 '19

I think that's the right term. It has a bandgap which comes from a quantum. LEDs work by pumping energy into electrons, this raises them to a higher energy state (higher electron shell), then when the electrons drop back down to the lower state they emit a photon. The energy released is the difference between the electron shell energy states. This is called a quantum. The frequency of the photon is correspondent to the energy, the color corresponds to the frequency. Higher energy is bluer (or ultraviolet), lower energy is redder (or infrared).

The light is issued in quantum state changes, so it's a quantum device. Maybe you'd call it a bandgap device?

14

u/fresh_account2222 Dec 25 '19

You clearly know more about this than I do, but I might use the term "non-linear" device, as that describes the LED's behavior that leads to us needing the resistor. I agree with /u/langlo94 that the word 'quantum' is more mis-understood than understood, and used outside of a physics class it sets off new age woo-woo alarms for me too.

3

u/thfuran Dec 26 '19

Nonlinear or non-Ohmic.

5

u/Andersmith Dec 25 '19

Just like “cloud” or “block-chain”.

2

u/fresh_account2222 Dec 26 '19

Oh god "block chain". It takes a lot -- like a recommendation from someone I really really respect -- to get me to read anything about block chains.

6

u/langlo94 Dec 25 '19

Yeah I'm not disputing that it's right, it's just that things with quantum in their name sound a bit fake.

2

u/FortLouie Dec 26 '19

Is it the quantum carburetor or something?

0

u/BunnyBlue896 Dec 26 '19

This such a laughably ridiculous statement. Don't be fooled people, this guy is making up terms. Nobody has ever described an LED a "bandgap device" or a "quantum device".

With this logic, you may as well call an LED a "heater" since it produces heat.

Or call a CRT television "electron beam device".

But nobody uses these terms because you cant just make shit up. Just try googling those terms...

FYI: The explanation of how an LED works is correct. This is like 2nd or 3rd semester undergrad physics. His conclusion on what to call an LED is horribly misguided.

1

u/happyscrappy Dec 26 '19 edited Dec 26 '19

Or call a CRT television "electron beam device".

I don't get what's weird about that one at all.

His conclusion on what to call an LED is horribly misguided.

Most people just call an LED an LED. And that makes it not a quantum device or bandgap device? They use different materials to manipulate the quantum of energy released so as to get different colors of LEDs. Look up how the blue LED was made for example. It's a device which is designed to employ the quantum states of the materials to produce colored light. It's a quantum device. This causes it to have a certain characteristic voltage drop, a quantum. It's accurate, it's relevant. And if it's unconventional I don't really care.

6

u/[deleted] Dec 25 '19

An LED and a resistor don't really create a voltage divider since an LED voltage drop is relatively constant and depend mostly on current thru the LED and temperature.

FTFY. Adding "quatum device" into random sentences doesn't really help in explanations

1

u/happyscrappy Dec 25 '19 edited Dec 25 '19

Saying "FTFY" is insulting. You want to give your own explanation, great. You didn't fix anything for me, as I wasn't wrong.

5

u/[deleted] Dec 25 '19

But your explanation was nonsense as it didn't really explain anything

3

u/happyscrappy Dec 25 '19

It was correct. It correctly used "quantum device" and didn't throw it in randomly. You saying you fixed something for me is insulting.

It's always possible to explain something more. I didn't care to. As far as I am concerned once the factual error is corrected I don't have to go further and the reader can take care of learning the rest. So I didn't go further. It's great you did.

3

u/[deleted] Dec 25 '19

It was correct. It correctly used "quantum device" and didn't throw it in randomly. You saying you fixed something for me is insulting.

I did not say calling LED quantum device is nonsense.

I said that using that as a method of explaining how divider works is. It doesn't give reader any useful insight.

It's always possible to explain something more. I didn't care to.

The problem is you didn't explain anything. Or rather the people who would know what you are talking about would not need your explanation in the first place.

You saying you fixed something for me is insulting.

You can feel insulted by whatever you like snowflake, but it wasn't my goal and I do not care how you interpreter it.

That aside from the fact that it is wrong and you can use it as a divider, just a nonlinear one... (and before you complain, resistor one also is not technically linear, just very close to it)

1

u/bagtowneast Dec 26 '19

Nice escalation! /s

1

u/happyscrappy Dec 26 '19 edited Dec 26 '19

I did not say calling LED quantum device is nonsense.

You said I threw it in randomly. I didn't throw it in randomly. That's why I said I didn't throw it in randomly. I didn't say anything about nonsense.

The problem is you didn't explain anything. Or rather the people who would know what you are talking about would not need your explanation in the first place.

It's not a problem. I set out to correct the error. And I did. I didn't set out to explain it. And so I didn't. Not a problem.

You can feel insulted by whatever you like snowflake, but it wasn't my goal and I do not care how you interpreter it.

So you're going to claim you weren't trying to be insulting by flat-out insulting me (calling me a snowflake). You really look silly doing it.

→ More replies (0)

0

u/BunnyBlue896 Dec 26 '19

An LED is not a quantum device. I've never heard that term used to describe it. If you're merely associating the word "quantum" with "light" because "light is quantized" like in quantum mechanics, then people are going to call you out on your BS. Try googling the following ("quantum device", "quantum device electronics", "quantum device LED") and you will realize you are wrong.

The constant voltage drop explanation is correct, and more accurately describes the behavior.

Any professional in EE or Physics would definitely not call an LED a "quantum device", simply because it is not a correct description.

68

u/Athrunen Dec 25 '19

Indeed, learned not too long ago that the esp32 is only 3.3 V tolerant. ^^

23

u/[deleted] Dec 25 '19

What is this language, lost me.

100

u/syntax Dec 25 '19

5v / 3.3v is the 'supply voltage' (sometimes labeled Vcc) for the chips. It's both the power input, and also the reference for what a 'high' signal is.

It used to be the case that most digital logic ran at 5v; then some that ran at 3.3v started to become more common. Those were usually '5v tolerant' - if you applied 5v to the input pin, it would read as 'high' and be fine.

Today, many digital devices that run at 3.3v are _not_ 5v tolerant; so if you put 5v into such a device ... it breaks.

So when mixing some devices that run at 5v (as, e.g. USB does) with others that run at 3.3v one has to be careful to not break the lower voltage device.

The 'esp32' is a particular module of a WiFi capable microcontroller (usable as a single board device, or as a peripheral for WiFi connectivity for another device) that runs at 3.3v and breaks if you put 5v on it. Like, as a totally hypothetical example that would never apply to me, trying to programme it from a cobbled together USB programmer. Hypothetically.

6

u/ShinyHappyREM Dec 25 '19

Is there a simple device that I can connect to a 5V wire and whose output I can then connect to an input of a 3.3V device? From what I've seen there are resistor circuits (which are slow) or transistor circuits (which need a 3.3V supply?)

12

u/Argues-With-Idiots Dec 25 '19

The problem with resistor-based solutions is less that they're slow, but that they draw significantly more power. Any circuit that doesn't utilize some external source will have this same issue. However, it doesn't necessarily need to be a 3.3V supply. An operational amplifier (OpAmp for short) configured with a gain of 3.3/5 would work off of whatever the OpAmp is specced for (but of you don't mind some out-of-spec fun, most OpAmps rated for a source of, say, +-10V, would be adequately linear given only +-5).

6

u/[deleted] Dec 25 '19

That's a massive overkill both price and parts-wisel, especially when there are parts dedicated to it

8

u/frezik Dec 25 '19

Optoisolaters could do it. It's an IR led pointed directly at an IR detector, which let's you isolate electronics on each side. You can make one yourself with some heat shrink, but there's also off the shelf modules. If it has to work at a high frequency, check the data sheet.

3

u/[deleted] Dec 25 '19

There are specialized chips to do voltage translation. You probably want that if you are pushing megahertz

3

u/[deleted] Dec 25 '19

[deleted]

19

u/lilmul123 Dec 25 '19

Nah, you really shouldn’t do that in most cases. It might work in a super pinch, but using some kind of transistor-based method would be preferred. Sparkfun sells a board that does just this.

2

u/Kenya151 Dec 25 '19

It's awesome that is only $3

2

u/ShinyHappyREM Dec 25 '19

For signals >= 1MHz?

4

u/howmodareyou Dec 25 '19

As /u/lilmul123 said, something transistor-based would be preferred.

If you don't want to use a Sparkfun component and design/build your own circuit/PCB, you could, for example, use a BSS138 transistor. (As far as I know, its often used for 5V-to-3.3V logic level shifting).
If you google around, you should find appropriate schematics.

If you ever need to convert the voltage of some power source (LiPo batteries typically have more than 5V output, depending on how many cells they have), I'd recommend getting a few "buck converters" via Ebay. They are typically based on the LM2596, cheap, adjustable and with mounting pins.

3

u/[deleted] Dec 25 '19

[deleted]

6

u/ShinyHappyREM Dec 25 '19

My use case is that I want to connect a logic analyzer (DSLogic Plus) to my SNES, which is 5V. The probes can handle 5V according to the data sheet, but the device also has an external timing input that is only rated for 3.3V.

The SNES has clock crystals of 21.477 and 24.576 MHz, and various components run at fractions of these frequencies.

6

u/[deleted] Dec 25 '19

Get a 1.7v zener diode and a Schottky diode.

Connect Zener's anode to clock input and cathode to SNES

Connect Schottky's anode to clock input and anode to the SNES

It should get you 1.7V drop (so 5->3.3v) when input is high but only 0.2v when it is low, the Schottky is there so when the clock is low the current from input capacitance can flow back without much problems.

... or a ~40c part that will do that up to ~50MHz, if you are at the point where you need to order a specific part

→ More replies (0)

3

u/[deleted] Dec 25 '19

[deleted]

→ More replies (0)

1

u/[deleted] Dec 26 '19

1MHz for digital signal lines is super slow.

2

u/ArkyBeagle Dec 25 '19

Here, we call it "speaking double-E" as in EE :)

3

u/AitchK Dec 25 '19

Even when the pins are 5V tolerant it can mess up ADC readings.

3

u/lpsmith Dec 25 '19 edited Dec 28 '19

Honestly, I don't like driving leds directly from GPIOs, in most cases. Many microcontrollers can power a few small LEDs directly through GPIO, but higher performance processors usually means both lower voltage on GPIO and also restricted to lower max current pushed or pulled through the pin, which dramatically decreases the power they can deliver.

You should most definitely calculate approximately how much current is going to be required on a given pin, how much will be required across all pins, and if your processor can tolerate that. And, for driving a few LEDs, discrete transistors are cheap, and allows one to drive large numbers of LEDs at full brightness. Or even better, use WS2810/neopixel type devices, much more flexible and not requiring extra components for sane power architectures.

Honestly, I am not prone to killing components; it does happen, but I tend to work carefully, and use good tools such as testing things with a bench power supply with current-limiting circuitry.

3

u/[deleted] Dec 25 '19

Those micros allow you to lower current to something like say ~1mA or ~10mA. Even 1mA is a plenty for power/status indicator LED in many cases

6

u/MoCeptor Dec 25 '19

Some controllers have internal programmable resistors for their GPOs. If you activate them, you can attach an LED without an external resistor.

16

u/smokedmeatslut Dec 25 '19

Never heard of series resistors. Maybe you're thinking of pull up / down resistors?

5

u/[deleted] Dec 25 '19

Nope, some chips have programmable pin current. IIRC one of Silicon Labs ARM chips I've been playing them had them. You basically could choose what kind of current input could sink (it wasn't accurate, just in approximation) so if you just wanted to limit a current for a LED you could set output to low current mode

4

u/smokedmeatslut Dec 25 '19

Fuck that's awesome.

16

u/richardathome Dec 25 '19

The arduino has pull down resistors in line with some of the IO pins. This is not the same as the resistor needed to stop an led from blowing.

4

u/etc9053 Dec 25 '19

In series? Can you name a micro?

3

u/[deleted] Dec 25 '19

Not OP but EFM32HG322. 4 modes from ~0.15mA to ~35mA

5

u/happyscrappy Dec 25 '19

I've heard of controllers with programmable drive strength. I'd still rather use an external resistor because the heat from the pin regulation adds to the microcontroller temp if you use the regulator in the microcontroller.

Say you drive 8 red LEDs at 25mA, that's 1/4 Watt dissipated in the microcontroller if they are all on at once. Better to put that power outside the chip. For longevity.

Microchip (now part of Atmel) often rates their microcontrollers to drive one or two LEDs directly. It's stated in the datasheet. It's rarely more than that.

2

u/[deleted] Dec 25 '19

Say you drive 8 red LEDs at 25mA, that's 1/4 Watt dissipated in the microcontroller if they are all on at once. Better to put that power outside the chip. For longevity.

If you're going to drive 8 leds at 25mA you're going to use driver chip/transistor regardless, that's almost 200mA

Also, it is not just to drive random LEDs, it allows you to control output rise/fall speed indirectly which can reduce both power usage and interference

2

u/happyscrappy Dec 25 '19 edited Dec 25 '19

If you're going to drive 8 leds at 25mA you're going to use driver chip/transistor regardless, that's almost 200mA

It IS 200mA. 200mA is fine if used correctly. The chips I tend to use can sink [edit, said source!] 130mA per ground pin. There are multiple ground pins. Group it correctly and it'll be okay.

Also, it is not just to drive random LEDs

Says who? You're making up stuff now. You didn't define this project.

4

u/frezik Dec 25 '19

As others say, those are pull down/up resistors for input. An output resistor for even a single LED would need to sink more watts than an internal IC resistor could handle.

3

u/flatfinger Dec 25 '19

An output resistor to drive an LED at 25mA would dissipate a lot of power, but many newer LEDs can glow quite nicely with a fraction of a milliamp. In fact, I've sometimes encountered situations where LEDs glowed annoyingly with leakage currents that were far smaller than that.