Show Posts
Pages: 1 ... 53 54 [55] 56 57 ... 110
811  Using Arduino / General Electronics / Re: 3.3v + 5v = 8.3v? on: December 19, 2012, 06:14:50 pm
Nope. Voltage is meaningless without a reference.

The pin that says +5v, is actually saying it is +5V higher in potential than GND.
Similary the +3.3V is saying it is +3.3V higher than GND.

How then can you get 8.3v if both have the same reference point?

You would need some power converter circuit to isolate the two supplies to join them together to make 8.3v, by which point the whole thing has got so complex that you are better off getting a boost converter to step the +5v rail alone up to the 8v you require.
812  Using Arduino / Programming Questions / Re: Arduino's LCD library is using all interrupt pins (PORTB) on: December 19, 2012, 03:53:08 pm
Just change the pins in the declaration, e.g.

LiquidCrystal lcd(12, 11, 5, 4, 7, 6)

For the LiquidCrystal library it doesn't matter which pins you use.
813  Using Arduino / Microcontrollers / Re: Arduino Uno and AtTiny45 (8 Mhz) on: December 19, 2012, 10:11:42 am
Post your idea here, I'm not available outside the forum.
814  Using Arduino / Microcontrollers / Re: Arduino Uno and AtTiny45 (8 Mhz) on: December 19, 2012, 09:59:16 am
Could you try disconnecting the LED from the attiny and upload again just to see if that is interfering with programming.
815  Using Arduino / Microcontrollers / Re: Arduino Uno and AtTiny45 (8 Mhz) on: December 19, 2012, 09:46:55 am
You need a 10k pullup resistor  on the reset pin of the ATTiny (10k resistor from pin1 of ATTiny45 to +5v)
816  Using Arduino / Microcontrollers / Re: Arduino Uno and AtTiny45 (8 Mhz) on: December 19, 2012, 09:24:59 am
-F is an avrdude flag. Basically you cant set it with arduino, only from command line. But you don't need or want to set it, all it tells avrdude to do is to ignore any verification error and continue on regarless - this will NOT help as whatever the current problem is will still be there and you will just end up with a corrupted chip.

Could you post a picture of your wiring so we can check it. It sounds like there is a loose connection somewhere or you have connected something up to the wrong place.
Also could you go to File->Preferences, and then check the box for 'upload' next to 'show verbose output during'. Then try programming again and copy and paste here what the messages you get in the black window at the bottm.

Also, check you have the correct programmer selected (Tools->Programmer->Arduino as ISP) and the correct Serial port (Tools->Serial Port). Finally, make sure you have selected ATTiny45 as the board (Tools->Board), and that you are using 'Upload Using Programmer' (File->Upload using programmer).
817  Using Arduino / Programming Questions / Re: Correct method of creating library? on: December 18, 2012, 07:09:04 pm
Thats the correct indexing, yes.
818  Using Arduino / Programming Questions / Re: Correct method of creating library? on: December 17, 2012, 04:09:56 pm
i <<= 1;
Is a shorthand way of writing
i = i << 1;
Which says bit shift i one bit to the left and save back in i. For example if you have the binary number:


it is made of 8 bits (assuming it is a byte). If we shift it 1 bit to the left, what we do is essentially remove the 8th bit, and add a zero on the end:

remove 8th: 1000100
add a zero: 10001000

If you have a look at the two side by side:
Can you see that as the name suggests, all bits have shifted one space to the left (apart from the last bit which has been removed)?

Now to the (i&1). That says, do the bitwise and of the value in the variable 'i' and the literal number 1.
What is a Bitwise AND? Well, first you need to know what a logical AND is:
A B | Q
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1

If you take two inputs A and B, and then do the logical AND, what it says is 'the output is a 1 if and only if all inputs are a 1'

Now consider the bitwise AND. It is the same principal, but it applies it to all bits in a number. Lets say A=0b01011101 and B=0b11110000, then the calculation goes as follows:

11110000 &

Can you see that a bit in the output is a 1 if and only if both corresponding bits in the input are a 1.

Now lets put it together in the code I gave:
for (byte i = 1; i < 16; i <<=1){
  digitalWrite(bMotorCoil_1, (i&1));
  digitalWrite(bMotorCoil_2, (i&2));
  digitalWrite(bMotorCoil_3, (i&4));
  digitalWrite(bMotorCoil_4, (i&8));
Initially the number i=1 ("byte i = 1"). At this point i is less than 16, so we proceed with the loop.
(i&1) = 0b00001 & 0b00001 = 0b00001. This is not 0, so is condered HIGH.
(i&2) = 0b00001 & 0b00010 = 0b00000. This is 0, so considered a LOW
(i&4) = 0b00001 & 0b00100 = 0b00000. This is 0, so considered a LOW
(i&8) = 0b00001 & 0b01000 = 0b00000. This is 0, so considered a LOW
So for that loop, only the first is set high.
At the end of the loop, i is bit shifted 1 to the left. So i becomes:
1<<1 = 0b00001 << 1 = 0b00010 = 2

 At this point i is again less than 16, so we proceed with the loop.
(i&1) = 0b00010 & 0b00001 = 0b00000. This is 0, so considered a LOW
(i&2) = 0b00010 & 0b00010 = 0b00010. This is not 0, so considered a HIGH
(i&4) = 0b00010 & 0b00100 = 0b00000. This is 0, so considered a LOW
(i&8) = 0b00010 & 0b01000 = 0b00000. This is 0, so considered a LOW
So for that loop, only the second is set high.

And so on. If you work through you will see in the next loop only the 3rd is high. In the fourth loop only the 4th is high.
At the end of the 4th loop, i = 8. i is then bit shifted 1 to the left. So i becomes:
1<<1 = 0b01000 << 1 = 0b10000 = 16
This is not less than 16, so the loop exits.
819  Using Arduino / General Electronics / Re: Sensing negative voltage on: December 16, 2012, 05:21:57 pm
Just for completeness, attached is the proposed circuit.

The two values 'R' are the same, but the exact value depends on what sort of load resistance you can put on your signal. Ideally they should be no more than 32kOhm, which would present 64kOhm to your signal source.
820  Using Arduino / Programming Questions / Re: Code - Help Required - Xmas Display Pong Game on: December 16, 2012, 04:40:15 pm
The byte is an improvement, but 'rows' can't be declared const as it is changed by 'void pattern()'
821  Using Arduino / Programming Questions / Re: Correct method of creating library? on: December 16, 2012, 04:28:40 pm
This is not possible:


You *could* (but dont have to) simplify this:
  /* Pulse each of the stepper motors coils in a sequential order */
  digitalWrite(bMotorCoil_1, HIGH);
  digitalWrite(bMotorCoil_2, LOW);
  digitalWrite(bMotorCoil_3, LOW);
  digitalWrite(bMotorCoil_4, LOW);
  digitalWrite(bMotorCoil_1, LOW);
  digitalWrite(bMotorCoil_2, HIGH);
  digitalWrite(bMotorCoil_3, LOW);
  digitalWrite(bMotorCoil_4, LOW);
  digitalWrite(bMotorCoil_1, LOW);
  digitalWrite(bMotorCoil_2, LOW);
  digitalWrite(bMotorCoil_3, HIGH);
  digitalWrite(bMotorCoil_4, LOW);
  digitalWrite(bMotorCoil_1, LOW);
  digitalWrite(bMotorCoil_2, LOW);
  digitalWrite(bMotorCoil_3, LOW);
  digitalWrite(bMotorCoil_4, HIGH);
To this:
for (byte i = 1; i < 16; i <<=1){
  digitalWrite(bMotorCoil_1, (i&1));
  digitalWrite(bMotorCoil_2, (i&2));
  digitalWrite(bMotorCoil_3, (i&4));
  digitalWrite(bMotorCoil_4, (i&8));
822  Using Arduino / General Electronics / Re: Sensing negative voltage on: December 16, 2012, 04:25:04 pm
Potential divider is the simplest approach. (EDIT: as Magician has said.)
823  Using Arduino / General Electronics / Re: Newbie step-labeled JPG - Looking for pointers on current flow on: December 11, 2012, 03:47:22 pm
From your diagram, I believe the circuit you have made is what I have drawn in the attachment.

Lets work through what everything does.

The Motor and Diode
  A motor is a device which converts electrical energy into mechanical energy. There are many types, but you have a small DC motor. For such a motor, if you apply a sufficient voltage, the motor will spin. Lets say that sufficient voltage is 5v, as that is what you are using in your circuit. If you connect +5v to the red wire, and 0v to the black wire there is 5-0=5V across the motor and so it will spin. If you reverse the red and black leads it will spin in the opposite direction as you now have 0-5=-5V across it.
  The problem with these motors is they have some 'inductance' associated with them. The inductance comes from the fact that the motor is setting up a magnetic field which is what is moving the rotor. Now the voltage across an inductor is equal to its inductance multiplied by the rate at which the current flowing through it is changing. If you change the current slowly or not at all, the voltage is very low or zero. But if you change the current very quickly, the voltage is high.
  Now consider what happens if you disconnect the supply from the motor instantly (pull a wire out of the breadboard). If the motor had current flowing through it, the current has instantly stopped as you pulled out the wire. This means that the rate of change of current is theoretically infinite. Based on that, the inductance in the motor suddenly has infinite voltage across it. Now infinite voltage sounds a totally daft concept, and thankfully it doesn't happen. Instead you get a very high voltage which causes a spark as current rushes through the air from one terminal of the motor to another. To protect the motor from this arcing, what we can do is put a 'freewheeling diode' or 'flyback diode' across the motor.

  A standard diode acts like a one way street. Current can only from the Anode (A) to the Cathode (K), and not the other way around. If you connect the diode as shown in the diagram (ignoring the transistor at the bottom for now), can you see that current will not flow through it? (The anode is at 0V, the cathode at +5V, so it is reverse biased - in other words current wants to flow from the +5v to the 0v, but can't as that would require it to flow from the cathode to the anode). So normally in the circuit below, the diode wont conduct.
  So how can we use this to protect the motor? Well, when the supply is removed, the inductor wants to keep the current moving so that it doesn't have infinite voltage across it. As such it drives a current which can now flow from the '-' terminal of the motor, through the diode (going from anode to cathode, the direction a diode conducts), and back to the '+' terminal of the motor. This current decreases as the energy stored in the inductance decreases, until eventually the inductor is discharged and the current stops. Hurray, the supply can be switched off instantly and the motor is protected from sparks.

The Transistor
  Basically the transistor is a cross between a switch and an amplifier, and really how it works depends on what transistor type you have. I have drawn a BJT (Bipolar Junction Transistor), such as the 2N2222A or BC547 and many others. I am guessing you have the former as I found a page which references the tutorial I believe you followed.
  The way a BJT works is that if a current 'B' flows into the base (and out of the emitter), a larger current 'C' flows into the collect and out of the emitter. These two currents are proportional to each other.So we can say that:

C = H x B

Where H is the 'DC current gain' of the transistor. This value depends on the voltages and currents you are using and is generally not linear, but lets for the say of simplicity say H=180. This is about correct for the value of current I will use below according to the datasheet.

Lets say that the motor requires 77mA to flow through it in order to run. Looking at the circuit, that means we need 77mA to flow from the +5v line, through the motor, and into the collector of the transistor. Based on what we have said about currents in the transistor, this means we need a current flowing into the base of:

B = C/H = 77 / 180 = 0.43mA

There is one more thing that you should know about BJT transistors and that is that in order for it to switch on, you need approximately 0.7V between the Base and the Emitter. That is where the resistor in the diagram comes in.

The voltage across a resistor is defined by 'Ohms Law' as:
V = I x R
Voltage = Current x Resistance.

For this circuit, we need 0.7V across the base, and 0.43mA flowing into it. As such we need a resistor which will converter a Logic 1 of +5v to the values we need.
If we want 0.7V at the transistor, that means the resistor has to drop 5-0.7=4.3V. Furthermore, we need 0.43mA to flow into the base, and in this circuit the only place it can come from is from Pin9, through the resistor and to the base. So we need 0.43mA to flow through the resistor.
Using Ohms Law we get:
R = V/I = 4.3/0.00043 = 10000 Ohms, or 10kOhms.

What happens if we put a logic 0 on pin 9. Well, you will have 0V at pin 9, which means there is 0V dropped across both the resistor and the transistor - think of it as a hill, to top of the hill is at 0V, the bottom of the hill is at ground (which is 0V), so the height is 0.
If there is 0v across the resistor, then you have:
I = V/R = 0/10000 = 0mA flowing into the base.
If there is no current flowing into the base, then there is:
C = 0 * 180 = 0mA flowing into the collector.
No current flowing into the collector means no current flowing through the motor, which means the motor is turned off.

I know I went through that kind of quickly, and there are a lot of words, but if you go over it a couple of times, having a look at the schematic attached, then feel free to ask about anything that is unclear.

(*Disclaimer: some of this post has been over simplified, but that is to make the overall concept of the circuit clearer)
824  Products / Arduino Due / Re: Obscure infinite for loop. on: December 09, 2012, 06:04:30 pm
Just to add some spice on this thread, another differences between AVR and ARM is that ARM is a 32bit processor, so, if you are used to work with char due to program size limitations, now with the Due, I guess you will need to change your habits,

Read here: Efficient C Code for ARM Devices, also here for a different point of view.

Enjoy and have a nice day.

--- Ricky
That makes for interesting reading. It may also explain why the Due runs my Nokia LCD slower than an 16MHz AVR. I think I may have to add many more #if #else statements.

That improved the speed slightly (switching all chars to ints, and unsigned chars to unsigned ints), though it is still slower, perhaps it is due to the differences between digitalWrite() for the Due, and direct port access for AVR.
825  Using Arduino / General Electronics / Re: What are the advantage and disadvantage of Darlington pair on: December 09, 2012, 03:53:13 pm

Higher switching current


Multiple transistor stages means more noise, and possibly a slower response. Though this is somewhat meaningless without any specific part.

An example - you haven't given any information on purpose, so I will make up a unrealistic example to demonstrate...

For a BJT (Bipolar Junction Transistor), the current that flows into the collector is proportional to the current that flows into the base.
For example transistor x has a Beta, or current gain, of around 30. What this means is that for every 1mA that flows into the base, 30mA can flow into the collector. This is fine for smallish control signals such as that from an arduino pin. The arduino can source 20mA which means for this gain you can have a maximum collector current of around 600mA.
But what if you need to drive say 20 x 0.45A loads individually. That would require you to source ~17mA form every pin which would go far beyond the maximum power dissipation of the chip. Instead what if you used two transistors for each load. The base of the first is connected to the the arduino via a resistor to control the base current. Its collector is connected to Vcc, and its emitter is connected to the base of a second transistor. The second transistor is used to drive the load.
In this configuration lets say you put 0.5mA into the base of the first transistor. This gets amplified so that 0.5*30=15mA flows into the collector and out of the emitter from the power supply. This 15mA flows into the base of the second transistor where it is again amplified and into the second transistors collector you get 15*30=450mA.
By using this configuration for all of the loads you have cut down the amount of current the microcontroller has to source from 300mA down to 20mA, which is much better for the microcontroller.

Basically the darlington pair (or triple, or quadruple etc) allow you to get higher gains in current than a single transistor. But the whether they are needed or not depends on the application.
Pages: 1 ... 53 54 [55] 56 57 ... 110