Hi to all
Im after some help on a project with arduino.
its a conversion from a frequency based Sensor to a voltage based sensor.
So the voltage sensor goes from 0 to 5v
the ecu needs to read this as frequency where 0v=160hz and 5v=80hz square wave.
and display on screen input voltage and output frequency.
Is arduino right for this type of project.
Thanks for reading..
Tony
/*
Voltage to Freq Conveter 1Bar for NA
Reads an analog input on pin A5, converts it to voltage and Level, and prints the result to the serial monitor.
Attach the center pin of a MAP Sensor to pin A5, and the outside pins to +5V and ground.
*/
#define DEBUG
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define timerPin D4 // LCD Read goes to Analog 0
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 1023; // sensor maximum, discovered through experiment
// Variables will change:
int TFTState = 0; // current state of the TFT
int lastTFTState = 0; // previous state of the TFT
int inputValue = 0; // Map Sensor input variable 0-5v
int outputPin = 11; // set output pin for Frequency Output 160 to 80Hz
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
pinMode(outputPin, OUTPUT);
#ifdef DEBUG
Serial.println(F("MAP Sensor Converter...Loading OK"));
Serial.println(F("TFT LCD test"));
Serial.print("TFT size is ");
Serial.print(tft.width());
Serial.print("x");
Serial.println(tft.height());
#endif // DEBUG
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
#ifdef DEBUG
Serial.println(F("Found ILI9325 LCD driver"));
} else if(identifier == 0x9328) {
Serial.println(F("Found ILI9328 LCD driver"));
} else if(identifier == 0x7575) {
Serial.println(F("Found HX8347G LCD driver"));
} else if(identifier == 0x9341) {
Serial.println(F("Found ILI9341 LCD driver"));
} else if(identifier == 0x8357) {
Serial.println(F("Found HX8357D LCD driver"));
#endif // DEBUG
} else {
#ifdef DEBUG
Serial.print(F("Unknown LCD driver chip: "));
Serial.println(identifier, HEX);
Serial.print(F("I try use ILI9341 LCD driver "));
Serial.println(F("If using the Adafruit 2.8\" TFT Arduino shield, the line:"));
Serial.println(F(" #define USE_ADAFRUIT_SHIELD_PINOUT"));
Serial.println(F("should appear in the library header (Adafruit_TFT.h)."));
Serial.println(F("If using the breakout board, it should NOT be #defined!"));
Serial.println(F("Also if using the breakout, double-check that all wiring"));
Serial.println(F("matches the tutorial."));
#endif // DEBUG
identifier = 0x9341;
}
tft.begin(identifier);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin A5:
int sensorValue = analogRead(A5);
// map the sensor range for freq:
// Invert and convert to Frequency 160-80hz:
int range = map(sensorValue, sensorMin, sensorMax, 160, 80);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// do something depending on the
// range value:
// print out the value you read to Serial
Serial.println("");
Serial.println("MAP Sensor Converter");
Serial.print("KPa Absolute");
Serial.println(voltage * 20.32);
Serial.print("");
Serial.print("Voltage");
Serial.println(voltage);
Serial.print("Freq Hz");
Serial.println(range);
// do something with outputs from input
// print out the value you read to TFT Screen Display
// save the current state as the last state,
//for next time through the loop
lastTFTState = TFTState;
tft.fillScreen(0x000000);
tft.setTextColor(0x0007FF);
tft.setTextSize(3);
tft.setCursor(0, 30);
tft.println("1 Bar Map");
tft.setCursor(20, 60);
tft.println("KPa Absolute");
tft.setCursor(100, 90);
tft.println(voltage * 20.32 );
tft.setCursor(0, 140);
tft.println("Voltage");
tft.setCursor(20, 170);
tft.println("");
tft.setCursor(100, 200);
tft.println(voltage);
tft.setCursor(0, 250);
tft.println("Freq Hz");
tft.setCursor(100, 280);
tft.println(range);
}
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
If it is too big, attach it as an ino file.
Thanks.. Tom...
What's your question or what's the problem? You've written a lot of code, are you seeing what you expect on the LCD display?
The tone() function will put-out a square wave at the specified frequency.
You can simplify things by maping the raw 0-1023 ADC reading to frequency without converting to volts. (But of course, if you want to see volts on your display you'll have to make the conversion.)
You might need a delay()... If you're running a fast-tight loop, you could end-up calling tone() over-and over before one 80-160Hz cycle has completed, and I don't know how it would respond to that... I'm just thinking that it might be a good idea to let tone() run for 1 second or more before you re-start it at the same or different frequency.
I cleaned it up added delay, output and Tone commands.
how does that look.
Im after a square wave that has 50% duty cycle.
#define DEBUG
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
#define timerPin D4 // LCD Read goes to Analog 0
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 1023; // sensor maximum, discovered through experiment
// Variables will change:
int TFTState = 0; // current state of the TFT
int lastTFTState = 0; // previous state of the TFT
int inputValue = 0; // Map Sensor input variable 0-5v
int outputPin = 11; // set output pin for Frequency Output 160 to 80Hz
// the setup routine runs once when you press reset:
void setup() {
tft.reset();
uint16_t identifier = tft.readID();
if(identifier == 0x9325) {
identifier = 0x9341;
}
tft.begin(identifier);
}
// the loop routine runs over and over again forever:
void loop()
{
int sensorValue = analogRead(A5); // read the input on analog pin A5:
int range = map(sensorValue, sensorMin, sensorMax, 160, 80); // Invert and convert to Frequency 160-80hz:
float voltage = sensorValue * (5.0 / 1023.0);
tone(11, range, 50);
delay(1000);
noTone(11);
lastTFTState = TFTState;
tft.fillScreen(0x000000);
tft.setTextColor(0x0007FF);
tft.setTextSize(3);
tft.setCursor(0, 30);
tft.println("1 Bar Map");
tft.setCursor(20, 60);
tft.println("KPa Absolute");
tft.setCursor(100, 90);
tft.println(voltage * 20.32 );
tft.setCursor(0, 140);
tft.println("Voltage");
tft.setCursor(20, 170);
tft.println("");
tft.setCursor(100, 200);
tft.println(voltage);
tft.setCursor(0, 250);
tft.println("Freq Hz");
tft.setCursor(100, 280);
tft.println(range);
}
Doesn't the tone() function output a square wave?
For more direct control look into PWM style outputs - set it to 50%, vary the frequency. Maybe through the library, maybe through direct timer register manipulation.
Ok, thanks heaps for your help and ideas, this code works a treat and project is finished, unless testing brings up some problems, thanks again for your help
#define DEBUG
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
const int sensorMin = 0; // sensor minimum
const int sensorMax = 1023; // sensor maximum
// Variables will change:
int inputValue = 0; // Map Sensor input A5 variable 0-5v
int outputPin = 11; // set output pin D11 for Frequency Output 80 to 160Hz
void setup() {
lcd.begin(16, 2); // start the library
lcd.setCursor(0,1);
lcd.print("Output Hz"); // print message
lcd.setCursor(0,0);
lcd.print("Input Volts"); // print message
}
// the loop routine runs over and over again forever:
void loop()
{
int sensorValue = analogRead(A5); // read the input on analog pin A5:
int range = map(sensorValue, sensorMin, sensorMax, 160, 80); // Convert to Frequency 80-160hz:
float voltage = sensorValue * (5.0 / 1023.0);
tone(11, range, 50);
delay(200);
noTone(11);
lcd.setCursor(12,0); //
lcd.print(voltage); // display Input MAP Voltage since power-up
lcd.setCursor(10,1); //
lcd.print(range); // display Output Freq since power-up
lcd.print(" "); // clears the rest of line
}