Pages: 1 [2] 3   Go Down
Author Topic: Devide 12v square wave signal and output 5v squarewave  (Read 2638 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your input,

I'd like to  update the output sqaurewave as quick as possible, accuracy wise, an error margin of around 5% would be acceptable.

I've merged your code into mine, have I understood your code correctly?

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1

  square_out = square_in / 2 * ratio2 / idler * FD * pulses;



/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (gear_in == HIGH) {
Ahem!
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oops sorry smiley-roll forgot about that (again!)

Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


if (gear_in == LOW) {        //ratio1 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Where do you have the attachInterrupt()?
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oops sorry missed that out too. You can tell I'm a newbee smiley-roll
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);

attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


if (gear_in == LOW) {        //ratio1 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (gear_in == LOW) {     
Yup.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

would an "else" statement be more effecient or is two "if" statements ok?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An "else" usually indicates you know what you're doing, as does a "digitalRead".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am still learning, only had an arduino about 3 weeks, and before that never touched C, I have done some VB, but that was also self taught so I don't want to pick up too many bad habbits early on. So far I've learnt the little I know from the exmaples and from this forum.

I've updated my code with an else function. So now that I've got the frequncy stored into the varilable "sqaure_in" how do I output a frequency?
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_in = 2; //sqaure wave input from speed source
const int gear_in = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_in, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_in, INPUT);

attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


/*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
*/



  if (gear_in == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


else{        //ratio2 is selected
  square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


/*
  //output the variable "sqaure_out" as square wave to "Speed_out"
*/
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26631
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're still missing the digitalRead
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
God Member
*****
Karma: 27
Posts: 829
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An optocoupler may be your safest way to translate the voltage. Car voltages are not always clean 12V and can go as high as 18v. A voltage divider wouldn't protect against that. Since you will be driving an LED on the 12V side, just select a proper resistor to keep the current under the transmitter IF spec. Most I have looked at are around 50mA, so around 240 ohms (330 is probably a little safer which is about 36mA.)

Are you just using the arduino to translate the input pulses of 12v to output pulses of 5v or are you using the arduino to actually calculate the RPM? If the latter, I am not sure why you need to output the pulses on another pin.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're still missing the digitalRead
Code:
/*
Read square wave speed signal in on pin2 and output squarewave speed on pin4
 */

volatile uint32_t lastPulseTime = 0;
volatile uint32_t lastPulseWidth = 0;


const int speed_pin = 2; //sqaure wave input from speed source
const int gear_pin = 3;     //gear select input
const int speed_out = 4; //sqaure wave output

// variables for calculation
const float ratio1 = 1/ 2.315;
const float ratio2 = 1/ 1.731;
const float FD = 1 / 4.7;
const float idler = 1.04;
const float pulses = 4; //pulses per simultated revolution
float square_in; //speed received
float square_out; //speed to send
int gear; // current gear selected
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.

  pinMode(speed_pin, INPUT);
  pinMode(speed_out, OUTPUT);
  pinMode(gear_pin, INPUT);

  attachInterrupt(0, registerPulse, RISING);
}

// the loop routine runs over and over again forever:
void loop() {
  //Sample data to test
  square_in = 1000000UL / lastPulseWidth;


  /*
 read the square wave speed input from "speed_in" into variable "sqaure_in"
   */


  gear = digitalRead(gear_pin);
  if (gear == HIGH) {        //ratio1 is selected
    square_out = square_in / 2 * ratio1 / idler * FD * pulses;
  }//end of ratio1


  else{        //ratio2 is selected
    square_out = square_in / 2 * ratio2 / idler * FD * pulses;
  }//end of ratio2


  /*
  //output the variable "sqaure_out" as square wave to "Speed_out"
   */
}
void registerPulse() {
  uint32_t cur = micros();
  lastPulseWidth = cur - lastPulseTime;
  lastPulseTime = cur;
}


Sorry, I see what you mean now, I've now added a a digtialread for the gear. I don't understand how I can get the frequncy from pin2 into the registerpulse function??
Logged

UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An optocoupler may be your safest way to translate the voltage. Car voltages are not always clean 12V and can go as high as 18v. A voltage divider wouldn't protect against that. Since you will be driving an LED on the 12V side, just select a proper resistor to keep the current under the transmitter IF spec. Most I have looked at are around 50mA, so around 240 ohms (330 is probably a little safer which is about 36mA.)

Are you just using the arduino to translate the input pulses of 12v to output pulses of 5v or are you using the arduino to actually calculate the RPM? If the latter, I am not sure why you need to output the pulses on another pin.

the output from th ECU is regulated to 12v, however, I've just googled optocoupler and it looks that it could be a good idea, I assume these a fast enough?

I'm using the arduino to calculate the RPM and then output a different RPM based on another input (gear selector)
Logged

Florida USA
Offline Offline
Newbie
*
Karma: 2
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Another way would be to clamp the 12 volt pulse to the + 5volt supply with either a fast switching diode(1N4148) or a small signal schottky diode(1N5711) ..It would require a series resistor from the 12v pulse source to the diode clamp,then another voltage divider to drop the approx 5.5 volt pulse to a safer 4.5 volt level.It would mean a few more components but should work.I never did this so if someone thinks of a problem with this please jump in.
jolphil
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 112
Posts: 5286
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following formula moves your floats to integers (better uint32_t, precision: 3 digits behind the point):

Code:
square_out = square_in * 201UL / 1138UL ; // case ratio1
square_out = square_in * 420 / 1777; // case ratio2

Output can be:

Code:
uint32_t lastEdge = 0;
byte state = 0;

void loop() {
  uint32_t outPulse = 500000UL / square_out; // get pulse length (not wave length)
  uint32_t m = micros();
  if (m - lastEdge >= outPulse) {
    state = ! state;
    digitalWrite(out_pin, state);
    lastEdge = m;
  }
}

I would optimize a bit more and do the whole calculation only if the lastPulseWidth value changed.
Logged

Pages: 1 [2] 3   Go Up
Jump to: