Better resolution for inclinometer

Hi, I have a 4-20ma inclinometer which i am going to use as a position for a telescope. the current program i have works great but only gives me a whole number not a decimal deg angle. the resolution of the sensor said it can be a max of 0.001 deg. and the sensor is very stable. How do I modify my code to have better resolution.

inclinometer.ino (467 Bytes)

If you wanted to reach the largest audience, you'd post your code.

does the attachment not work?

Here is the code i'm using.

const float analogInPin = A0;

float sensorValue = 0;
float outputValue = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 143, 1023, 30, 90);
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print(" DEG ");
Serial.print(outputValue);

delay(1000);

Attachment "works", but a lot of people on mobile devices can't open them.

Please remember to use code tags when posting code.

"map" is an integer function.
Try fmap instead.

Thanks i think i have it now. here is what i did.
Sorry about not putting tags on code im new to all of this and was not aware :confused:

const float analogInPin = A0; // assigns analogInPin as A0


float sensorValue = 0; // The sensor value
float sensorMin = 143;  // Minimum sensor value
float sensorMax = 1023;// Maximum sensor value
float outMin = 30; // Min value in degrees 
float outMax = 90; // Max value in degrees 




void setup() {
 
Serial.begin(9600); // sets the serial baud rate 
}

//This scales the sensor value to the max and min values in a decimical number.

float fmap (float sensorValue, float sensorMin, float sensorMax, float outMin, float outMax)
    {
  return (sensorValue - sensorMin) * (outMax - outMin) / (sensorMax - sensorMin) + outMin;
     } 
void loop() {
 
//  reads the analog value and assigns it to the sensor value

sensorValue = analogRead(analogInPin); 

//maps the sensor value to be a deciminal number between 30 and 90
 
sensorValue = fmap (sensorValue, sensorMin, sensorMax, 30, 90);

//Displays the sensor value as Degrees
 
Serial.print("DEG = ");
Serial.print(sensorValue);
  Serial.print("   DEG ");
  Serial.print(outputValue);

delay(1000); 
}

You can often increase effective sensor resolution by oversampling and averaging.

In this case, one approach would be to add up 64 ADC samples (into an unsigned it) and divide by 16, which may work to give 12 bit resolution.

Overview here.

Use the map function after average, or not at all -- scale the result yourself.

DO NOT use float variables for pin numbers, use byte or int.

const float analogInPin = A0; // assigns analogInPin as A0

sparksincode:
does the attachment not work?

Would you download some code, I wrote, to your computer and run it?

Idahowalker:
Would you download some code, I wrote, to your computer and run it?

Very true, didn't even think about the security side of things.

You might give thought to using a Optical Flow Sensor to track a guide star to create torque rotations for your scope. Using the same principle of a RigelSys Finder scope. A Guide star can be centered on the Optical Flow Sensor. As the guide star drifts off position of the sensor a correcting torque value can be created.

A servo using 500 to 2500uS PWM for 0-180ish degrees makes ~5.54uS per degree.

Greater degrees of torque values can be had by using uSeconds.

If the range of the servo is limited, say 90 degrees of travel, the degree per uSec becomes 11.11uSec per degree.

The smaller you can make your start stop per servo torque the less blurring you will have with imaging.

As an addition:
The ESP32 torques a servo by clock ticks, the formula to convert servo torques to clock ticks on the ESP32 comes to a even integer of 3 clock ticks per uSec. 3Clcok ticks per uSec gives tight control on servo position. I use a ESP32 to torque servos.

Idahowalker:
You might give thought to using a Optical Flow Sensor to track a guide star to create torque rotations for your scope. Using the same principle of a RigelSys Finder scope. A Guide star can be centered on the Optical Flow Sensor. As the guide star drifts off position of the sensor a correcting torque value can be created.

A servo using 500 to 2500uS PWM for 0-180ish degrees makes ~5.54uS per degree.

Greater degrees of torque values can be had by using uSeconds.

If the range of the servo is limited, say 90 degrees of travel, the degree per uSec becomes 11.11uSec per degree.

The smaller you can make your start stop per servo torque the less blurring you will have with imaging.

As an addition:
The ESP32 torques a servo by clock ticks, the formula to convert servo torques to clock ticks on the ESP32 comes to a even integer of 3 clock ticks per uSec. 3Clcok ticks per uSec gives tight control on servo position. I use a ESP32 to torque servos.

I only have a 10Inch manual Dob that I i'm attempting to make into a push to scope that will interface with stellarium. Once i have this incllinometer code sorted ill will be getting a encoder to sort out the Azimuth then send the data into stellarium.
you dont happen to know how to interface with stellarium via the serial port? would i need some sort of virtual serial port to interface with it and what format do I have the alt/az data.
I assume I would be able to figure this out eventually when i get to it.

This sort of programming is all very new to me and the only programming I have done is ladder logic on Allen Bradley PLC's. but I love to learn and this day and age it is a matter of "googling" it and bluffing your way through the nitty grittys.

0.001 degrees is astounding! I thought I was doing well with angle sensors getting close to 0.01 degrees.

If you compress 360000 points of resolution into 4-20mA then you need an analog-to-digital converter capable of resolving nano-amps. Something with more than 19 bits of resolution. The 10-bit analogRead() is hopelessly outclassed here.

At that level, you need to pay close attention to all sources of noise: electrical, magnetic and even mechanical, since walking nearby the telescope will move it a detectable amount. Wiring and PCB layout are critical.