I am building a motor controller for an electric car project. The Arduino Uno accepts input from a drive by wire throttle pedal and sends out a PWM signal (I am still perfecting this). The throttle wire is connected to pin A0. The Arduino reads the resistance on the throttle pedal and varies the PWM output, hence controlling the motor speed. So far so good.
What I would like to do is add a temperature sensor (like a DS18B20) that reads what's going on in my power stage (IGBT bricks) and can reduce or shut off power if it exceeds a certain value. Hypothetically, if the sensor exceeds 200 degrees F, I would want the Arduino to reduce power by 50% until the temps drop (reduce PWM by 50%), and if it hits 220F, I want a shutdown== no PWM signals at all.
Can I configure a second serial device to feed that temp value, so I can assign it to a variable and use it to affect the PWM? How do I get the second serial device incorporated?
There exist temperature sensors with SPI or I2C interface for easy use with any Arduino.
That sensor has a 1-wire interface, not Serial.
Please learn more about the available communication protocols before you start ordering sensors. Only buy sensors that come with a data sheet and Arduino library or at least Arduino example code.
What is the frequency of your PWM signal? Are you using analogwrite() function to create PWM signal?
You can use OnwWire.h Library to acquire temperature signal from DS18B20 sensor. A typical connection diagram is shown in Fig-1; where the LEDs (L and LED2) will monitor the statuses of temperature (L will turn ON temp == 200 F and LED2 will turn ON when temp >220 F).
Your sketch of post #6 is presented below with code tags (</>).
int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
potValue = analogRead(potPin);
motorValue = map(potValue, 145 , 880, 0, 255);
analogWrite(motorPin, motorValue);
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("t motor = ");
Serial.println(motorValue);
}
1. You are acquiring analog signal using A0-pin for Ch-0 of the internal ADC of MCU.
2. You are using DPin-9 to generate 490 Hz PWM signal for Motor.
3. DS18B20 uses (Fig-1 of post #4) uses 1-Wire Interface and NOT "Serial/UART Interface" for communication. You may upload the following sketch to do the functional check of your temperature sensor.
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); //Sensor signal pin is connected with DPin-2
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
void setup(void)
{
Serial.begin(9600); // start serial port
sensors.begin(); // Start up the library
pinMode(5, OUTPUT); //DPin-5 is output to drive LED2
pinMode(13, OUTPUT); //DPin-13 is output to drive L (built-in LED og UNO)
}
void loop()
{
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperature from Sensor: ");
float myTemp = sensors.getTempCByIndex(0);
Serial.print(myTemp, 2);//2-digit after decimal point
Serial.println(" *C");
delay(1000); //test interval
}
My temp sensor arrived today, I wired it up, and ran this sketch. The motor value drops appropriately when the temp exceeds my limit (set to 100 for purposes of testing in my lab):
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(2); //Sensor signal pin is connected with DPin-2
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
int potPin = A0;
int motorPin = 9;
int potValue = 0;
int motorValue = 0;
void setup(void)
{
Serial.begin(9600); // start serial port
sensors.begin(); // Start up the library
pinMode(5, OUTPUT); //DPin-5 is output to drive LED2
pinMode(13, OUTPUT); //DPin-13 is output to drive L (built-in LED og UNO)
}
void loop()
{
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print("Temperature from Sensor: ");
float myTemp = sensors.getTempCByIndex(0);
float myFTemp=myTemp*9/5+32; //Convert to Faherenheit because why not
Serial.print(myFTemp, 2);//2-digit after decimal point
Serial.println(" *F");
delay(1000); //test interval
potValue = analogRead(potPin);
motorValue = map(potValue, 145 , 880, 0, 255);
if (myFTemp>100) motorValue=motorValue/2; //Cap motor output when temperature exceeds whatever value, TBD
analogWrite(motorPin, motorValue); //Send that junk to the power stage
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("t motor = ");
Serial.println(motorValue);
}
Please edit your post and add code tags. Better still, Auto Format the code in the IDE to help show its structure and post it here in code tags to make it easy to navigate and copy for examination