analogWrite() works for pulsing speedo pin but digitalWrite() doesn't.

I have a speedometer that i need to pulse to output a value (around 2.41HZ per km). I can get it to output around 100km/h using the base dimming sketch from the IDE, but if i try to do

void loop() {
    digitalWrite(6, HIGH);
    delay(10);
    digitalWrite(6, LOW);
    delay(10);
}

absolutely nothing happens. If it matters, I could get the speedo to output a value using a 555 timer.

Don't provide code snippets.

"around 2.41HZ per km" makes little sense. Perhaps you intended 2.41 Hz per km/hour ?

Did you forget pinMode(6, OUTPUT);?

int speedoPin = 6; 

void setup() {
    pinMode(speedoPin, OUTPUT);

void loop() {
   digitalWrite(speedoPin, HIGH);
   delay(10);
   digitalWrite(speedoPin, LOW);
   delay(10);

This is pretty much my entire code, i'm at the stage of messing around and figuring how things work so i don't have much yet.

vaj4088:
"around 2.41HZ per km" makes little sense. Perhaps you intended 2.41 Hz per km/hour ?

Yes, that's what i meant. Sorry for being unclear.

You say that it works using analogWrite()

Please post the full code for that version

What exactly is connected to the output pin ?
Which Arduino board are you using ?

v3s:
This is pretty much my entire code

It's missing '}' in two places. Perhaps the problem is in the other parts you aren't showing.

20 milliseconds of delay will get you about 50 Hz so about 20 kph?

Did you remember to connect both Ground and Pin 6 to your device?

v3s:
I can get it to output around 100km/h using the base dimming sketch from the IDE,

What do you mean by "base dimming sketch"?
The PWM pins are 490.20 Hz or 976.56 Hz. Shouldn't that give you 203.4 kph or 405.2 kph?

UKHeliBob:
You say that it works using analogWrite()

Please post the full code for that version

What exactly is connected to the output pin ?
Which Arduino board are you using ?

void loop() {
   analogWrite(6, 127);
}

This is all i need for analogWrite to output a value, but now i'm getting confused. When i was experimenting with a 555 timer, it really was around 2,41Hz per km/h. But now i'm sending the full 980Hz into the speedo, and getting around 120km/h, which is weird. I changed the pin to 9, and i'm getting the same result.
I'm using an Arduino Nano, and i connected the signal input pin to the output of the Arduino. I don't know anything about the inner workings of the speedo, because it's an old speedo and i cant find anything about it on the internet. I only have the pinout.

johnwasser:
It's missing '}' in two places. Perhaps the problem is in the other parts you aren't showing.

20 milliseconds of delay will get you about 50 Hz so about 20 kph?

Did you remember to connect both Ground and Pin 6 to your device?
What do you mean by "base dimming sketch"?
The PWM pins are 490.20 Hz or 976.56 Hz. Shouldn't that give you 203.4 kph or 405.2 kph?

The code on the arduino is fine, since the missing }'s are just from me manually retyping the code since i'm using a different device for programming. I connected all the pins fine from what i can see, since it works on analogWrite. And the base dimming sketch i was talking about is this one.

/*
  Fading

  This example shows how to fade an LED using the analogWrite() function.

  The circuit:
  - LED attached from digital pin 9 to ground.

  created 1 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Fading
*/

int ledPin = 6;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

And regarding the PWM pins, that's now something im thinking about. It really shouldn't output only 120km/h at those frequencies. I kind of wanna try sending 12V as a signal instead of 5, but im worried about breaking the speedometer.

On the UNO,
Pins 5 and 6 are the 976.56 Hz pins.
Pins 3, 9, 10, and 11 are the 490.20 Hz pins.

If your speedometer is responding to frequencies, Pin 6 and Pin 9 should have produced different results. Could they be above the frequency range for the speedometer? Maybe the speedometer DOES use 2.41 Hz/kph and but limits values above 200 kph? What is the upper limit on the speedometer?

Sorry for disappearing for a bit, was needed somewhere else. The upper limit is at 220kph, so 530Hz, but pin 9 can output 490Hz so it should show 203kph. Now I've noticed that the results do differ between pin 6 and pin 9, but the difference is around 10-20kph.

Okay, i somehow fixed it... I really have no clue what i did, but after some tinkering it started working. As for the speed limitation, turns out it was the fact that i was powering the speedo with 5V instead of the intended 12V, and after switching supplies it started working properly (i now power it through a MOSFET, gonna try switching to a transistor as the speedo signal takes 0mA so it's a waste of a mosfet). Thanks for trying to help me along the way!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.