0

I have an Arduino Uno and a 555 timer configured to astable mode.

What I'm trying to do is to program my Arduino to increase a "counter" variable (counter++) everytime it gets triggered by 555 timer's output (through analogIN pin).

My problem is that Arduino takes 555 timer's output as a continuous triggering and increases my counter as long as the pulse in the analogIN lasts. So instead of incrementing by 1, it is increased by approximately 97 (sometimes more or less).

I need to take the analog in as a "packet" so everytime analogIN is triggered, no matter how long the pulse lasts, it will be treated as a single event and not as continuous triggering.

What do I have to change?

My code:

int counter = 0;

void setup() {
  Serial.begin(9600);


}

void loop() {
  if(analogRead(A0) > 250) {
    counter++;
    Serial.println(counter);
  }

}

Thanks

  • Have your code remember the previous value, and only increment when the previous value was below 250, and the current value is above 250. – Gerben May 13 at 15:17
  • PS is there any reason why you are using analogRead, instead of digitalRead? – Gerben May 13 at 15:19
  • you are checking for high, but you assume that low will somehow be magically checked also – jsotola May 13 at 16:28
  • No, even when I checked for low it didn't work as expected – BrainTrance May 14 at 13:22
0

You need to create a finite state machine with 2 states:

  1. Waiting for input from 555 to go high. When this happens increment the counter & advance to the next state.
  2. Waiting for the input from the 555 to go low. When this happens advance to the next state. That is, the first state.

In the following Arduino "State Change Detection (Edge Detection) for pushbuttons" example / tutorial you do not need the complex code to debounce the switch. However, the edge detection and state machine portion of the code can likely be applied to your project.

  • It seems that edge detection is what I needed. Thank you! – BrainTrance May 14 at 13:00
  • This approach might lead to unreliable readings when many and/or time consuming tasks are handled in loop(). It might be a proper solution to read slow and single events like button activity, but i don't recommend it to read a 555 pulse signal. – Sim Son May 15 at 7:02
0

I recommend using one of the atmega's counter modules. That way you can count the pulses synchronuously to your program (real paralellism!) and you also don't have to implement any edge-detection youself as the counter module already provides this feature.

A search for "pulse counter" will lead to many tutorials on how to do that.

  • Edge detection seems to work fine. What is the difference between edge detection and your solution? is it faster? more reliable? – BrainTrance May 14 at 13:21
  • Yes, it is both faster and more reliable because it is handled by a hardware module that is optimized for edge detection and runs in parallel to your program. The counter module btw also gets involved when generating harfware pwm. Instead of counting a clock's edge it just counts the edges of an input signal. – Sim Son May 15 at 6:53
  • @BrainTrance, the HW solution is good. If you are new to Arduino or embedded programming it may be a bit difficult. But should result in a more robust solution. That said, do concern your self with directly using HW. It may be the HW resource of interest is already being used by other software such an included Serial Communication Arduino Library or LED Brightness Controller Arduino Library. – st2000 May 15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.