Unstable analogRead


I’m working on a project involving power regulation, and I’m having trouble getting stable ammeter readings.

What I see is a massive jump om the analog readings (see the Serialmonitor) “amps, A and outVal”

Also I attached a general description of the circuit.

Do i actually need a OPamp over the shunt resistor (although this will limit the upper amperage measurement). “I am planning on a measurement of at least 0.1 - 35.0A”

float volt;
int Vin;
int Ain;
float amps;
float ohm;
int watt;
float A;
int outVal = constrain(outVal, 0, 255);
const int battPin = A0;   // the pin that the Voltage sensor is attached to
const int ampPin = A2;    // the pin that the Amperage sensor is attached to
const int  buttonPin = 2; // the pin that the pushbutton is attached to
const int mosPin = 5;    // the pin that the MOSFET is attached to

void setup() {
pinMode(battPin, INPUT);
pinMode(ampPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(mosPin, OUTPUT);

void loop() {
 //Sensor calibration
Vin = map(analogRead(battPin), 430, 563, 6400, 8400); //2S Lithium, 22k & 10k
volt = (Vin/1000.0);
Ain = analogRead(ampPin);
A = map(Ain, 0.0, 1023.0, 1.0, 5000.0);
amps = (A/100);
watt = (volt*amps);
ohm = (volt/amps);

if (digitalRead(buttonPin) == LOW) {
  analogWrite(mosPin, outVal);
  if (amps < 0.5) {
  else {
else {
  digitalWrite(mosPin, LOW);
Serial.print(amps); //Debug
Serial.print(Ain); //Debug




Need a pulldown resistor from the gate of Q2. Suggest 22K from Gate to GND.