How do I configure an Uno Rev 3 to accept serial inputs from 2 devices?

Hello,

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?

Thank you

You might get away with using GitHub - PaulStoffregen/AltSoftSerial: Software emulated serial using hardware timers for improved compatibility for another set of pins and regular software serial for the other set of pins.

You could use a MCU with several hardware serial ports.

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.

@thaddeusthebold

Are you using A0-pin as an analog input pin?

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).

ds18b20-4
Figure-1:

Test Sketch
... pending.

The one example of the use of the DS18B20 explicitly uses the serial interface. My question was generated based on that.

Thank you for attempting to help.

This is what I'm running so far:

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);
}

That page does not show a serial interface. The sensor is connected to pin 2. The code clearly shows

#include <OneWire.h>

Which in not a serial interface as @DrDiettrich said.

It uses Serial only to display output. It uses OneWire and the DallasTemperature library for reading the sensor.

Are you facing difficulties to develop codes for the operation of the DS18B20 sensor?

nope.
Using Serial in this example has nothing to do with DS18b20 sensor.
Or please point exact code block where the DS18B20 sensor used via Serial

Ok, let's say the first serial is used for the DS sensor (although this is incorrect). What is the second one for? To read the signal from pin A0 ? :slight_smile:

Aha! Thank you.

I haven't even begun coding the sletch because I thought there might be a hardware limitatin on the Arduino Uno. I may start fiddling with it now...

@thaddeusthebold

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.
pwm328

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
}

That's awesome! Thank you.

Would 'myTemp' be an integer value that I could use as a trigger to arithmetically reduce my motorvalue? So:

(hypothetically)

If myTemp > 200 motorValue =motorValue/2

Do casting and then take the integer part from the float type variable myTemp. For example:

int temp = (int) myTemp;
if(temp == 220)
{
      //take action as needed
}

What for?
It is not recommended to use float in equality test, but in greater than less comparison you can use float directly:

 if(myTemp > 220)
{
      //take action as needed
}

OP wants to work on integer value.

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); 
 
}

Thanks to everyone for the help.

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