Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Code for a blinking LED with a timer on ATmega8 on: August 05, 2011, 11:31:30 am
The problem is solved finally. A colleague just helped me out with this great code.
So for anybody with the same problem who finds this thread via the board search or google, I post the code below.

It is actually like this:
Code:
void setup()
{
    pinMode(13,OUTPUT);
   
    // Disable interrupts while loading registers
    cli();
   
    // Set the registers; don't leave in the stuff with which
    // Arduino has initialized them.
    TCCR1A = 0;
    // Set mode
    TCCR1B = (1 << WGM12);
   
    // Set prescale values. (Could be done in same statement
    // as setting the WGM12 bit.)
    TCCR1B |= (1 << CS12) | (1 << CS10);
   
     // TIMSK1 for ATmega328, TIMSK for ATmega8
    TIMSK1 |= (1 << OCIE1A);
   
    // Set OCR1A after the mode selection, not before.
    OCR1A = 15624;
   
    // Enable global interrupts
    sei();
}

So, the prescale value in TCCR1B, which is in this case 1024 (see ATmega8 datasheet page 98), and the value in the Output Compare Register OCR1A define the frequency of the blinking LED on port 13:
Code:
ISR (TIMER1_COMPA_vect) {
  PORTB ^= _BV(PB5);
}
PB5 is the actual name of Arduino's pin 13. Everytime Timer1 hits OCR1A the function ISR (TIMER1_COMPA_vect) { } will be executed. In this case it's togglin pin 13 every second. Notice it's CTC-Mode which means the timer will be cleared every time the interrupt happens (it only counts up to the value in OCR1A and not the the full 16 bit which would be 65536).^

Greetings

Alex
2  Using Arduino / Programming Questions / Re: Code for a blinking LED with a timer on ATmega8 on: August 04, 2011, 10:17:38 am
Quote from: madworm
1) using digitalWrite() inside an ISR is a baaad thing. You want an ISR to finish quickly.
Quote from: madworm
Code:
ISR (TIMER1_COMPA_vect) {
  PORTB ^= _BV(PB5);
}
I was also using this snippet in my sketch for the UNO, in which I could simply attach the timer with TimerOne. But I wanted to eliminate every possible source of errors so I chose this style, which is much more accessible for me (as long as I'm testing).

Quote from: madworm
2) where is your #include <interrupt.h> at the top?
Arduino doesn't notice me about missing anything. The size of the compiled file is also the same if I put "#inclue <avr/interrupt.h>" on top or not. So, is this really necessary inside the Arduino enviroment?

Quote from: PaulS
For values of 0 or 1, you need an int?
No, of course not. smiley-wink I also could have taken a string. Just joking - as just mentioned above, I wanted to make it more accessible. Thank you for your coding style advice though.
3  Using Arduino / Programming Questions / Code for a blinking LED with a timer on ATmega8 on: August 04, 2011, 07:50:46 am
Edit: The solution is four posts below


Hey there,

my goal is to pulse an IR LED at 38 kHz but I still occupy myself with blinking LEDs visibly for debugging.
So the following sketch should do the same like the blink example: turning the LED on/off every second - only with a timer.

I want to do this with timers because I don't know any other method that is as accurate as timers. I need this accuracy because the IR reciever's sensitivity drops at 40 % if the transmission frequency is about +/- 10 % wrong. What a pity.

I've got an ATmega8 (still on a breadboard). (I burned the bootloader on it with the UNO as "Arduino NG or older w/ ATmega8")
Every sketch worked as it should except the examples including timers.

This is the code I am talking about:
Code:
volatile int val=0;

void setup() {
  pinMode(13,OUTPUT);
  
  OCR1A = 15624;
  TCCR1B |= (1 << WGM12);
  // Mode 4, CTC on OCR1A
  TIMSK |= (1 << OCIE1A);
  //Set interrupt on compare match
  TCCR1B |= (1 << CS12) | (1 << CS10);
  // set prescaler to 1024 and start the timer
  sei();
  // enable interrupts
}

void loop() {
}


ISR (TIMER1_COMPA_vect) {
    if (val == 1) {
      digitalWrite(13, HIGH);
      val = 0;
    } else {
      digitalWrite(13, LOW);
      val = 1;
    }
}

I found this example right here (in C):
https://sites.google.com/site/qeewiki/books/avr-guide/timer-on-the-atmega8
It looks pretty good to me so I expected it to work.

But the LED doesn't light up unfortunatley. (It has nothing to do with the circuit because the blink example works on the same pin)

I would be very thankful if you could tell me where I have a mistake in the code.
Or what else is wrong. Or any other ideas smiley-wink

Thank you very much

Alex

Btw. the TimerOne library worked pretty well for me as long as I was working on the UNO. But unfortunatley it only is constructed for ATmega168/328 smiley-sad
4  Using Arduino / General Electronics / Re: What did I not understand about transistor amplification? on: July 23, 2011, 03:48:08 am
Quote from: retrolefty
A Logic level mosfet is designed to full turn-on with just +5vdc on it's gate lead
Oh thank you sir, this might be just the thing I was seraching for!

Quote from: retrolefty
Here is an example of a nice logic level n-channel mosfet with very beefy voltage and current ratings that can be driven directly by a arduino digital output pin.
http://www.sparkfun.com/products/10213
I couldn't find this part at my mail order of choice (I'm here in Europe) but I'm sure to finde the right thing with the datasheet you gave me.

Thank you very much!
5  Using Arduino / General Electronics / Re: What did I not understand about transistor amplification? on: July 23, 2011, 03:11:27 am
Quote from: retrolefty
Definitely not. Once the base voltage is higher then the base emitter junction voltage there is nothing to control the amount of base current flowing from the arduino output pin without a resistor. This will certainly rise to more then the absolute maximum output pin current rating of 40ma and will then proceed to burn out the output pin driver circuitry. Your circuit components are what determines current flow according to ohm's law, so the base resistor is a required component.

Okay, I thought about about this now for a while. Maybe you could help me along with the mathematics.
In the datasheet of the 2N6488, which I'm using, it says the Current Gain is somewhere between 5 and 20 at 4 V (with Ic = 5 A up to 15 A). So if I want 4 V / 40 mA from my Arduino pin, I'll need a 25 Ohm resistor, right? Than I get like 200 mA through the collector... But I want something around 3 A.

So, does this mean I need a series of transistors until I get the right Collector current? I guess yes. Just asking to be on the safe side.

Is there maybe an alternative component to this? I don't know if you recognized it but at the beginning of the thread I mixed up the characteristics of transistors and relays. But I guess there is nothing like a relay what can switch its states at 13 us for about some milliseconds.

Would be great to hear your advice.
6  Using Arduino / General Electronics / Re: What did I not understand about transistor amplification? on: July 21, 2011, 02:01:33 pm
Quote from: retrolefty
That make sense?
Thank you for your detailed explanation. I had to read it four five times, but yes, I get it. smiley And it works great now.
(Don't know if anyone's interested but the IR LED now is so "bright", the reflection from the wall through a half opened door is enough to trigger the reciever... And I'm very satisfied now!)

So thank you very much you both!
7  Using Arduino / General Electronics / Re: What did I not understand about transistor amplification? on: July 21, 2011, 01:14:29 pm
Quote from: pwillard
1) you have a high value resistor when 1K is better.  You want the transistor to conduct.
I think now I get it. I've just tried resistors from 1K over 500 down to 150 Ohm and the current was increasing. The the conduction depends on the voltage on the base. That's the part I didn't understand. I was believing it behaves like discrete 1 and 0 gate with the npn/pnp transistors.

One step further: Could I simply hook up the base to the Arduino pin without any resitors?

Quote from: pwillard
2) You have connected the Emitter and Collector differently. [...] Essentially, let the Collector feed the load... (LED), connect Emitter to GND potential.
Alright, I didn't know this is important.

Quote from: pwillard
3) you do not have a current limiting resistor on the LED.
Yes, I wanted the LED to go poof. But in my application I have a resistor for my IR LED.

Thank you so much, you really really helped me a lot in understanding this topic.


Quote from: pwillard
In short, I feel that you have less understanding about NPN transistors than you believe you do.
You were so right...
8  Using Arduino / General Electronics / Re: What did I not understand about transistor amplification? on: July 21, 2011, 11:58:42 am
Thank you very much so far for your long answer.
I'm glad you try to help me!

Quote from: pwillard
1) Never a good idea to short circuit a transistor, regardless of rating.
Why not?

Quote from: pwillard
2) I doubt you will find this exact configuration you have in ANY amplifier designs
Yes, I know, I only build this to measure the current.

Quote from: pwillard
3) Never assume there is some "inner resistance" in a transistor.
How else would you tell it?

Quote from: pwillard
In short, I feel that you have less understanding about NPN transistors than you believe you do.
That's right I don't know much about any component. Very new to it.

Quote from: pwillard
Are you sure you mean "amplify"?
As you say it... No? I'm sorry, my English has got rusty. I don't want to amplify something like an audio signal. I just want more power than the arduino can provide. Like you say, an ON/OFF SWITCH . I'm using a timer interrupt to switch the LED ON/OFF every 13 us to achieve the 38 kHz the reciever needs. It already works on my breadboards but not with the desired current.

Quote from: pwillard
How about we give an example of how you can get an NPN to amplify?  Good place to start?   Maybe I will draw something and do a follow up post.
It would be great if you could help me to solve my problem. smiley

Quote from: pwillard
A problem you have right now is this.  A transistor can SWITCH ON and OFF and it can amplify... sometimes it can both... but it's related to base biasing.  Using just 1 resistor won't really do it for you.  You *can* make a voltage amplifying switch this way, (if the collector V+ voltage is higher) though there are some issues with how you have things connected.  in other words... you have sort of the right idea... but a less than optimal hookup.
Yeah unfortunatley I don't get what I have to change yet. Do you understand me at least? I want this IR LED to be pulsed at 38 kHz. In the datasheet there is a description that says that you can use it with 3 A (for 10 us in the example). Because you don't get that much from an arduino pin I wanted to use a transistor. But I only get about 100 mA.

Quote from: pwillard
Ok, rather than draw up something new... I'll give you a drawing I already created that does what you want.   Obvuously, change out my values of transistor and LED for yours.
At first view I would say I've done it this way.

So why do I get only 100 mA when I short circuit a transistor? How do I get the transistor to let the whole current pass?



Quote from: Erni
Do I have to use a MOSFET instead?
9  Using Arduino / General Electronics / What did I not understand about transistor amplification? on: July 21, 2011, 10:00:56 am
Hey there,

this my first transistor amplification and I'm running into some trouble of course.
In my original circuit I'm using a NPN transistor to pulse an IR LED with higher current than the arduino can provide.
The power source is 4 AAA batteries, so it's 6 V and up to 4,5 A (short circuit, limited by inner resistance).

The amplification was not that good so I made this easy circuit (attachment) to measure where the problem is.
When I got the transistor thing right there must be a high resistance between collector and emitter when there is no voltage on the base and nearly no resistance when I put voltage on the base. Right?

On my breadboard I have 30 mV when the base has no connection to anything and the full 6 V when I connect the base to +6V with a 10 K-Ohm resistor. That's cool.
But, now I'll describe my problem, there is only 100 mA flowing where the LED is. I measured it by taking the LED out, so there is only my multimeter, the transistor and the 10k resistor on the base. Nothing else. I expected the 4,5 A (which I had at the short circuit of the 4 AAA) but only got this 100 mA. Why?

The transistor is rated for 15 A / 80 V (it's a 2N6488). But I've also tested another npn with the same result.

So I would be glad if you could tell me what I didn't get about it. And how can I achieve to draw the full 4,5 A.

Greetings

Alex
10  Using Arduino / Programming Questions / Re: Question about the length of commands (how many microseconds...) on: July 18, 2011, 06:33:26 am
Quote from: robtillaart
let the Arduino give you the answer herself...
Thank you, I've tried this method and it turned out that I need 9 us to achieve 13 us with digitalWrite().

Quote from: D4p0up
First, digitalWrite() is costly in time. Better to use direct port writing to fasten your app!
Thanks for the hint, I'll check this out now.

Quote from: D4p0up
if you want to be accurate within thime, maybe you should have a look at the Arduino Interrupt capability
That would be a great method, but I think it will not work with my project. Correct me if I'm wrong but as far as I know you can turn on/off interrupts only for all the pins (with cli() and sei()). So I cannot get any ohter interrupts while my IR led is not transmitting (which I would need for a sensor in my circuit). (yeah I know, I could use a transistor for example, but this would be one more pin). So I think I better don't use interrupts.

Thank you both
11  Using Arduino / Sensors / Re: How can I pulse a ir led for a reciever? on: July 18, 2011, 03:55:12 am
Quote from: f.schiano
Thank you, that's a great source of information!

Quote from: tkbyd
There must be modules with a simple single digital input which causes a beam of modulated IR to shine when the input is taken high?
If an "expert" can point us to the right part numbers/ supplier, I for one would be grateful.
Now that I understand infrared transmission, it would not make sense to buy any expensive modules. I spent under 1 € (maybe about 1 $ ) for all the things like ir led, (power) resistor, transistor, etc. And it works great. (just like in the link above)
If I understood you right, you worried about making these things with the arduino because it takes calculating capacity? It would sure be nice to keep the code more clean but this transmission function only needs a few milliseconds in my case. Not a length of time that is very relevant when playing lazer tag smiley

Quote
I only mention it to save you the 30 seconds it took me to check out whether using "analogWrite()" would be an option. I suspect that if you want to delve into the internals of the Atmega chip (and risk upsetting things the Arduino system software expects) you could get the Atmega to generate the 38kHz... but that's not a route I would advocate!
Neither for me, I find it more comfortable to to it with digitalWrite() and delayMicroseconds() than using some kind of AVR code which I found with google and don't understand at all.


By the way it's nice you kept on discussing on this thread and posting links, very informative for me.
12  Using Arduino / Programming Questions / Question about the length of commands (how many microseconds...) on: July 18, 2011, 03:21:03 am
Hey there,

I'm working with an infrared transmission at the moment and asking myself which delay instruction I should give the arduino when pulsing the led.
It's about the thing that an ir led has to be pulsed at a high frequency so that the sensor is able to read the signal, in this case 38 kHz.

38 kHz is 26,31.. microseconds. So I want the led to turn on/off every 13 us. I use this code:
Code:
while (time+3>millis()) {
        digitalWrite(6, HIGH);
        delayMicroseconds(13);
        digitalWrite(6, LOW);
        delayMicroseconds(13);
      }
at the moment and it works. But I read at http://www.ladyada.net/learn/sensors/ir.html#testing_your_ir_detector that this has to look like this:
Code:
while (time+3>millis()) {
        digitalWrite(6, HIGH);
        delayMicroseconds(10);
        digitalWrite(6, LOW);
        delayMicroseconds(10);
      }
In this guide they used 10 us insted of 13 us with the explanation that digitalWrite() needs 3 us itself. Unfortunatley there is are further comments to this topic.

Can you confirm that? Is 10 us better than 13 us or something in between? Is there a reference for the duration of commands?

I would be very thankful if you could give me a hint.

Greetings

Alex
13  Using Arduino / Sensors / Re: How can I pulse a ir led for a reciever? on: July 13, 2011, 03:15:04 pm
Quote
Best of luck with this.
Thank you, you helped me very much. I'm now ordering the parts.
14  Using Arduino / Sensors / Re: How can I pulse a ir led for a reciever? on: July 13, 2011, 09:02:51 am
Quote
I would change the PWM frequency to 38KHz and use an analogue write to turn it on. Then use code like you put to pulse it. Do you want the pulses to carry any information? [...] You will see a signal on the digital pin you connect to the receiver, this will match the pulses you send, not the 38KHz modulation.

Alright, I think I already understood it shortly after I posted my question. The 38 KHz pulse is something like a carrier frequency (right?). So if I change the PWM to 38 KHZ, I could use this code:
Code:
analogWrite(outPin, 128);
delay(10);
analogWrite(outPin,0);
delay(10);
to have the ir reciever HIGH for 10 ms?

Quote
You need to provide a power supply that will supply that current. One problem with a capacitor is that the voltage drops as it discharges. The other is that you will need a physically very large capacitor.
I wanted to use something like a 9 V battery as power source. Is it possible to get current around 1 A out of it? I was afraid it doesn't work because of the inner resistance (don't know how high it is).

edit: I just tested it with an AA battery and it were about 1.5 to 1.7 A (and it did not get hot smiley-razz)

Quote
Given your basic state of knowledge would you not be better off copying some one elses design rather than trying to design your own system?
Yeah, I know, it would be easier... But I am too ambitious for it smiley-razz and not that bad smiley-wink

Thank you so far for your answer, it was really helpful.
15  Using Arduino / Sensors / How can I pulse a ir led for a reciever? on: July 13, 2011, 07:08:17 am
Hey there,

I've already done some projects with Arduinos but this topic is rather complicated for me.
The project is a lazer tag clone and I have problems to deal with the infrared transmission.

I would be very thankful if you could help me along with these issues.

1. The first thing I am curious about is how to get enough current for the led.
I chose a Osram LD 274 because it is cheap and nearly as "bright" as the Vishay TSAL 6100 which nearly everbody recommends.
In the data sheet there is the description that you could use it with 1 Ampere for 100 microseconds (at 2.5 Volts).
Because the Arduino does not provide that much current, I wanted to use a npn-transistor which can handle 1 A and a capacitor, which can provide 1 A.
Because I am very new to capacitors, could you give me a hint which type is the right for this task?

2. These ir recievers always have bandpasses (for example a 38 khz). How do I deal with this on the emitter side?
Am I writing the transistor base high/low at 38 khz, maybe like
Code:
digitalWrite(outPin,HIGH);
delayMicroseconds(26);
digitalWrite(outPin,LOW);
delayMicroseconds(26);
(because one oscillation at 38 khz is around 26 us)
Is this right? And could the capacitor-, led- and transistor-circuit handle this?

3. Which informations (and how) would I get on the reciever side with a 38 khz reciever (for example the TSOP 31238)?
I read something aboud pulseIn(). The description in the reference is really too short for me, I don't know if I got it.
Is there a possibility to make this with interrupts? Because the code should be able to take a shot even if it is playing a jingle or something.

Again, I would be very thankful for help. It is hard to deal with all the tutorials around because the english used is not very easy for someone who's mother tongue is not english like mine.

Greetings

Alex
Pages: [1]