LM35 temp sesnor equation for arduino uno which connected to matlab GUI - matlab

Hi I'm new in matlab GUI
I am trying to create an axes plot the temperature which comes from LM35 through arduino uno to matlab
I used the following code to read the analog voltage, readVoltage(a,0)
I get a values about 0.28 - 0.30 but I don't know exactly what this values exactly means is it the the real temperature/100 or what? I know there is an ADC inside arduino converts the input voltage to another range (0-1023) when I use analogRead() on the arduino side. Does it also work here or not? I confused about this thing when I should assume it is 0-1023 or directly get the reading.

The arduino ADC reads a voltage and outputs a number according to
the reference voltage
the bit width of the ADC
in this case I suppose that you are using the 5V reference and 10 bit mode, so
Vmeasured = NumberFromADC * 5V / 1024
Now, according to the LM35 datasheet the output voltage is
Vout = 10mV/°C * T
inverting the equation:
T = Vout / (10mV/°C) = NumberFromADC * 5V / 1024 / (10mV/°C) = NumberFromADC * 500 / 1024
(of course expressed in °C)
BTW I suggest you to change the voltage reference to an internal one, since the 5V are not stable and precise enough to have a good measuring system. More info here.
And, of course, if you change the reference voltage you will need to change the equation since the reference itself will not be 5V anymore.

Related

HC-SR-04 with STM32

SR04 ultrasonic distance sensor with stm32.
When I use it with STM32 I use it as bothedge with input capture. I measure the HIGH time of the Echo pin.
I thought it was a mistake
When I used Ardiuno with pulsein, I was dividing the time by 29.1 .My measurement was accurate enough, but when I divide it by 29.1 when using STM32, the measurement is very wrong. I did it by dividing by 58.2. This time I got more accurate values. What is the difference?
Thanks a lot.
It's right to divide the duration of the echo pulse by 58, as described in the datasheet as follows.
You can calculate the range through the time interval between sending trigger signal and
receiving echo signal. Formula: uS / 58 = centimeters or uS / 148 =inch; or: the
range = high level time * velocity (340M/S) / 2;
I tried to find an Arduino code which divides pulse duration with 29.1 or 29, I couldn't find it. Some posts computes the range by range=(duration/2)/29.1. I guess you've missed dividing duration by two. Double check your Arduino code, or post it if you want to be assured.

How to measure/know precisely the ADC reference voltage on STM32L052K6T6?

I am currenctly working on STM32L052K6T6.
I would like to know if there is an appropriate way to measure precisely the voltage used by the adc.
I read through the documentation that this voltage was on adc_channel17 but i have no idea on how to get it.
I also read there was a calibration Variable called VREFINT_CAL but again i did'nt see the process to use it properly.
I know this voltage is for me around 1.8V. But i need to know it untill 1.80000 at least to calculate accurate values of my sensor.
To program my MUC i am using Atollic, i did the basic pins configuration using STM32CubeMX.
The internal reference voltage is not used by the ADC. It is only used to measure the actual Vref voltage. Vref voltage depending on the version of the chip can be Vref+ or Vdda.
How to measure the actual Vref?
You need to measure the Vrefint (which is about 1.2V) and then using the simple math calcultate the Vref
Vrefint = Vref * (RAW_ADC / 4096)
So Vref = Vrefint * 4096 / RAW_ADC
or if you want to use VREFINT_CAL : Vref = 3 V * VREFINT_CAL / RAD_ADC
You have to do a calibration before measuring good value of channel VREF_INT.
HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);

Delay interfacing Arduino and Simulink

I am trying to read data from potentionmeter using an arduino microcontroller (tried both arduino UNO, arduino FIO) and using serial communication interface it to Simulink (I tried baud rates ranging from 57600-921600).
Here is the Arduino source code:
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
*/
#define ana_pin A0
void setup() {
analogReference(DEFAULT);
Serial.begin(57600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(ana_pin);
// print out the value you read:
Serial.print(sensorValue);
// delay(500); // delay in between reads for stability
}
I interfaced it with Tera Term software and there is instantaneous change of values corresponding to 3 V or 0V.
However when I tried to interface it with Simulink model using instrument control toolbox:
there is a 10 second lag when value changes from ASCII representation of 3V to 0V
The block sample time is 0.01 seconds and the model configuration parameters are adjusted accordingly (I tried it for 1 second or more and the delay still remains. Also, I was able to record data from another sensor and LPC1768 development board without any delay.
I also tried it with Arduino support libraries in Simulink:
And it seems that no data is received, as you can see from Scope1 in the png file the status signal is always 0. I have also attached hardware implementation properties from Arduino block in Simulink:
Could you help me understand what is going on and how to solve this issue?
#Patrick Trentin -
I get a delay of 4 seconds when I use baud rates of 230400, 460800 and 921600.
For baud rate of 57600, 115200 I get a delay of 10 seconds.
Thank you for pointing it out I did not focus on that previously.
However since I will be using the sensor in an application which accurately needs reading every 0.01 sec. I dont think I can work with 4 sec delay.

Time dependent resistance using SimScape Electrical foundation library in Matlab

I'm trying to model a large electrical network of resistors and capacitors in SimScape using Electrical foundation library blocks. One of the resistors needs to change its resistance as a function of time, say like this:
R=R0*(1-delR*(1-exp(-t))
where R is resistance, R0 is the initial resistance, delR is the factor by which I want the resistance to decrease and t is time. I can't figure out how to incorporate time variance at all in to the simscape model(as I can't seem to access a time variable). There is a variable resistance block available, but I'm facing the same problem there as well, as it seems to be built to vary with respect to voltage. I'm using Matlab 2016a.
You can either:
Use the variable resistance block in conjunction with the clock block to achieve this.
Build your own block (a good starting point is the source code for variable resistance). And use the time keyword:
https://www.mathworks.com/help/physmod/simscape/lang/time.html
Your equations could look something like:
let
R = R0 * (1 - delR * (1 - exp(time));
in
v == R * i;

Increasing/Altering Matlab-Arduino analogRead() sampling rate

I have been controlling Arduino from Matlab using ArduinoIO-Matlab interface. My current setup is I have 3 EMG Muscle Sensors (from Advancer Technologies) are connected to the Arduino at analog pin 1,2, and 3. Arduino is connected to Matlab. I am trying to collect data from these three pins simultaneously and store them in an matrix size 1000x3. My issue is the rate at which Matlab is sampling from the analog pin. It takes about 25 seconds to collect 1000 readings from the 3 pins simultaneously. I know arduino itself samples at a higher rate. Below is my code. How do I alter this to get a sampling rate of about like 1000 samples in 10 seconds ?
ar = arduino('COM3');
ax = zeros(1000,3);
for ai = 1:1000
ax(ai,:) = [ar.analogRead(1) ar.analogRead(2) ar.analogRead(3)];
end
delete(ar);
This is the time taken by the above code (profile viewer):
time calls line
< 0.01 1 3 ax = zeros(1000,3);
4
< 0.01 1 5 for ai = 1:1000
25.07 1000 6 ax(ai,:) = [ar.analogRead(1) ar.analogRead(2) ar.analogRead(3)];
1000 7 end
8
1.24 1 9 delete(ar);
Please let me know if there is something else that I need to clarify.
Thanks :Denter code here
You need to modify the arduino c++ code (.pde file).
In this code you should sample the signal as you prefer (1000 for example) and then transfer the sampled data to matlab using serial.writeln() method.
This will give you a sampling rate of ~3KHz (depending on alot of factors)...
The following very probably explains the result that you are seeing and why you need to do something like what Muhammad's answer suggests. While this reason was implied by his answer it was not spelt out so that others can avoid the 'trap'.
I do not have access to the underlying code and systems needed to check this answer with certainty. This answer is based on "typical methods" and has a modest chance of being sheer poppycock [tm], but the exact fit between observation and standard methods suggests this is what is happening. A very little delving by someone with the requisite system to hand will demonstrate if this is correct.
When data is sent one data sample at a time you incur a per-sample overhead significantly in excess of the time taken to just transfer the raw data.
You say it takes 25 seconds to transfer 3000 samples.
The time per sample = 25/3000 = 8.333 ms per sample.
Assume a 9600 baud data transfer rate.
The default communications speed is liable to but 9600 baud. This can be checked but the result suggests that this may be correct and making slightly different assumptions provides an equally good explanation.
Serial coms usually uses N81 format = 1 start bit, 8 data bits, 1 stop bit per 8 bit byte.
So 1 bit takes 1/9600 s
and 10 bits take 10/9600 = 1.042 mS
And sample time / byte time
= 8.333 / 1.042 = 7.997 word times.
In fact if you do the calculations without rounding or truncation, ie
25 / 3000 x 9600/10 = 8.000.... .
ie your transfer is taking EXACTLY 8 x 9600 baud word times per sample.
Equally, this is exactly 4 x 4800 baud or 2 x 2400 baud transfer times.
I have not examined the format used but imagine that to work with the PC monitor program the basic serial routine may use
2 x data bytes + CR + LF = 4 bytes.
That assumes a 16 bit variable sent as 2 x 8 bit binary words.
More likely = either
- 16 bits sent as 4 x ASCII characters or
- 24 bits sent as 6 x ASCII characters.
In the absence of suitably deep delving, the use of 6 ASCII words and a CR + LF at 9600 baud provides such a good fit using typical parameters that Occam probably opines that this is the best starting point. Regardless of whether the total requirement is 8 or 4 or 2 bytes, the somewhat serendipitous exact match between your observed data rate and standard baud rates suggests that this provides the basic reason for what you see.
Looking at the code will rapidly show what baud rate, data length and packing is used.