I have a Sparkfun 7 segment display. I checked all the segments first to make sure they work, and they do. It has 2 common anode pins, both going to +5V. The remaining 8 pins each get a 2.2k resistor and go to an individual pin on the Arduino. Here is my code.I'm trying to test all of the pins. Basically+5V goes to the pin and circuit is not complete because of pullup resistor but gets complete when to essentially ground it.
// Assigns an I/O pin on Arduino to an individual segment
//segments are grouped 4 to a port to not overload Arduino
const int seg1 = 8;
const int seg2 = 9;
const int seg4 = 10;
const int seg5 = 11;
const int seg6 = 14;
const int seg7 = 15;
const int seg9 = 16;
const int seg10 = 17;
void setup() {
//Gives +5V to keep pins from floating
pinMode(seg1, INPUT_PULLUP);
pinMode(seg2, INPUT_PULLUP);
pinMode(seg4, INPUT_PULLUP);
pinMode(seg5, INPUT_PULLUP);
pinMode(seg6, INPUT_PULLUP);
pinMode(seg7, INPUT_PULLUP);
pinMode(seg9, INPUT_PULLUP);
pinMode(seg10, INPUT_PULLUP);
}
void loop() {
//Brings all pins to GND to test from Arduino
digitalWrite(seg1, LOW);
digitalWrite(seg2, LOW);
digitalWrite(seg4, LOW);
digitalWrite(seg5, LOW);
digitalWrite(seg6, LOW);
digitalWrite(seg7, LOW);
digitalWrite(seg9, LOW);
digitalWrite(seg10, LOW);
}
I'm pretty sure it's a code problem because if I take a jumper wire and move it from my the I/O pin to a GND pin on the Arduino , then the segment will light up.
You have made the pins into input pins, but they need to be output pins. What is a little confusing about this is that you can digitalWrite() to an input pin, and you can digitalRead() from an output pin. So you're not getting an error message even though your code is not actually doing what you want it to.
If you digitalWrite() to an input pin, it either enables or disables the internal pullup resistor, depending on whether you wrote HIGH or LOW. If you digitalRead() from an output pin, it returns the pin's current value (HIGH or LOW).
Thanks. Originally I had them as outputs too. lol But I wanted to simplify my code by using the pullup resistor. I guess that only works on an input, not an output. Well it works now. Thanks again. Now to make it do something useful...
Learn about arrays and loops and you can shorten your code.
This should light the segments.
// Assigns an I/O pin on Arduino to an individual segment
//segments are grouped 4 to a port to not overload Arduino <<==== whaaaaaat?
const byte seg[ 8 ] = { 8,9,10,11,14,15,16,17 };
byte index = 0;
void setup() {
for ( index = 0; index < 8; index++ )
{
pinMode( seg[ index ], OUTPUT ); // defaults LOW, segment will be ON
// digitalWrite( seg[ index ], HIGH ); // turns the segment OFF when you uncomment the line
}
}
void loop()
{
}
GoForSmoke:
Learn about arrays and loops and you can shorten your code.
This should light the segments.
// Assigns an I/O pin on Arduino to an individual segment
//segments are grouped 4 to a port to not overload Arduino <<==== whaaaaaat?
const byte seg[ 8 ] = { 8,9,10,11,14,15,16,17 };
byte index = 0;
void setup() {
for ( index = 0; index < 8; index++ )
{
pinMode( seg[ index ], OUTPUT ); // defaults LOW, segment will be ON
// digitalWrite( seg[ index ], HIGH ); // turns the segment OFF when you uncomment the line
}
}
There are sets of pins on ports. 3 main ports. So the LED I have pulls 20mA off each pin and at 8 pins is 160mA with a max of 100mA per port. Therefore it's better to split it up so as not to burn out the Atmega.
A red led with a 220 ohm resistor pulls less than 20 mA, the recommended most current you should pull which makes for a very bright red led as it is. And you're using 10x the resistance?
Calculating an LED resistor value
LED resistor circuit An LED must have a resistor connected in series to limit the current through the LED, otherwise it will burn out almost instantly.
The resistor value, R is given by:
R = (VS - VL) / I
VS = supply voltage
VL = LED voltage (usually 2V, but 4V for blue and white LEDs)
I = LED current (e.g. 10mA = 0.01A, or 20mA = 0.02A)
Make sure the LED current you choose is less than the maximum permitted and convert the current to amps (A) so the calculation will give the resistor value in ohms (ohm).
To convert mA to A divide the current in mA by 1000 because 1mA = 0.001A.
If the calculated value is not available choose the nearest standard resistor value which is greater, so that the current will be a little less than you chose. In fact you may wish to choose a greater resistor value to reduce the current (to increase battery life for example) but this will make the LED less bright.
Ohms Law with a led and resistor in the circuit:
V_total - Vled_turn_on = R * I
Red led has the lowest visible light On Voltage at about 2V. I dunno about IR leds but I choke those extra.
5V - 2V = 220R x Amps
3 = 220 x Amps
Amps = 3 / 220 = 0.013636... = 13.6 mA = plenty bright without stressing led or pin, hurts my eyes indoors.
Using a 1k or more resistor tones it down and lets you run more leds (segments) with less current.
red913:
Thanks. Originally I had them as outputs too. lol But I wanted to simplify my code by using the pullup resistor. I guess that only works on an input, not an output. Well it works now. Thanks again. Now to make it do something useful...
Correct. An input needs a pull-up or pull-down resistor to give the input a "default" state when the load is not pulling the input in any direction, such as if a momentary switch is disconnected. An output doesn't need a pull-up or pull-down resistor because the output itself will be pulling the pin either high or low. An output pin is never floating.
I was going off the datasheet absolute max for current. I thought it said typical. I went back and reviewed it and it's max current is 20mA, not typical current. I measured it and I'm only getting 1mA to a pin and a total of 8mA sourcing the whole LED. Also I was reading a little tutorial on another website and the guy made his calculations on 20mA too. Perhaps that's why I never bothered to measure anything. They're still really bright though.
If 20mA is my max and I have 8 pins. 20/8=2.5mA of available current per pin So to be safe I'll stick with 2mA per pin. That works out to a 1K(closest size) resistor and pulling about 17.1mA total from my 5V pin. The next size resistor I have from there is the 2.2K I was using previously. So at this point from I'll stick with the brighter LED's I get off the 1k.
Thanks GoForSmoke for the info.
GoForSmoke:
A red led with a 220 ohm resistor pulls less than 20 mA, the recommended most current you should pull which makes for a very bright red led as it is. And you're using 10x the resistance?
Calculating an LED resistor value
LED resistor circuit An LED must have a resistor connected in series to limit the current through the LED, otherwise it will burn out almost instantly.
The resistor value, R is given by:
R = (VS - VL) / I
VS = supply voltage
VL = LED voltage (usually 2V, but 4V for blue and white LEDs)
I = LED current (e.g. 10mA = 0.01A, or 20mA = 0.02A)
Make sure the LED current you choose is less than the maximum permitted and convert the current to amps (A) so the calculation will give the resistor value in ohms (ohm).
To convert mA to A divide the current in mA by 1000 because 1mA = 0.001A.
If the calculated value is not available choose the nearest standard resistor value which is greater, so that the current will be a little less than you chose. In fact you may wish to choose a greater resistor value to reduce the current (to increase battery life for example) but this will make the LED less bright.
Ohms Law with a led and resistor in the circuit:
V_total - Vled_turn_on = R * I
Red led has the lowest visible light On Voltage at about 2V. I dunno about IR leds but I choke those extra.
5V - 2V = 220R x Amps
3 = 220 x Amps
Amps = 3 / 220 = 0.013636... = 13.6 mA = plenty bright without stressing led or pin, hurts my eyes indoors.
Using a 1k or more resistor tones it down and lets you run more leds (segments) with less current.
red913:
I was going off the datasheet absolute max for current. I thought it said typical. I went back and reviewed it and it's max current is 20mA, not typical current. I measured it and I'm only getting 1mA to a pin and a total of 8mA sourcing the whole LED. Also I was reading a little tutorial on another website and the guy made his calculations on 20mA too. Perhaps that's why I never bothered to measure anything. They're still really bright though.
If 20mA is my max and I have 8 pins. 20/8=2.5mA of available current per pin So to be safe I'll stick with 2mA per pin. That works out to a 1K(closest size) resistor and pulling about 17.1mA total from my 5V pin. The next size resistor I have from there is the 2.2K I was using previously. So at this point from I'll stick with the brighter LED's I get off the 1k.
20mA is the recommended maximum per pin with a total recommended maximum for the chip of 200mA.
20mA x 8 = 160mA which while safe for an UNO might be too much for the 7-segment display.
Go with what you can see well enough which is what you're doing anyway.
If you ever do need more power, use a pin through a good bit of resistor to control a transistor that can safely carry the extra. Or use a chip to do the same thing, I've used opto-isolators to control external power use before.
But check out that with motors, solenoids and like (inductive loads) there may be extra circuitry or other chips to use than with for example lots of leds.
GoForSmoke:
If you ever do need more power, use a pin through a good bit of resistor to control a transistor that can safely carry the extra. Or use a chip to do the same thing, I've used opto-isolators to control external power use before.
In this situation, where the entire circuit is self-contained, I wouldn't think an optoisolator would be needed. A basic transistor should be fine. I would only use an optoisolator for interfacing between circuits where I wanted to keep the ground potentials separate.
I have used optos where what could happen on the far side (due to someone going off his meds) was 'variable' and a very expensive PC was on the near side, so to speak.
I would use one in a line between car power and an MCU for signal/power-status purposes.
I do note that they are very simple to use and the ones I had then came 4 to the chip.
I don't recall needing resistors on the input side but that was about 20 years ago. Maybe I did.
But many transistors can take more current and have a lower purchase price even with resistors.
I've got Darlington Arrays for middling power with built-in protection diodes in nice tight packages but no FETs yet.
Last time I was around FETs they were extremely vulnerable to any static at all. "The man with the money in the FETs, sweats." is how it was explained to me.