Code Works correctly when powered by usb but not barrel jack

Im still new to setting up and using arduino and understanding how everything interacts, with that said,
I have an Arduino Uno R3 board and I have a rotational encoder wired to it along with an 16x2 lcd screen. The screen's function is to output the rpm of the motor.

At the moment when the board is powered by usb from a computer port the code is working correctly and displaying the rpm as i calculated it should but when the board is connected to power using the barrel jack the rpm on the screen increases without touching/switching anything else.

The code i have below is without the LCD screen code because its not messing up the code calculating rpm

Notes:

  • Barrel Jack is 9V output
  • Encoder PPR is 180

#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;

void setup()
{
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pull-up resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pull-up resistor

attachInterrupt(0, doEncoder, RISING); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}

void loop() {
encoder0Pos=0; // counts clicks per revolution on encoder
delay(1000); // delys the count one second
float rpm=encoder0Pos/(3); // claculates rpm
Serial.println(rpm); // displays rpm
}

void doEncoder()
{
encoder0Pos++;
}

The question i have is, with coding, do you have to accommodate the code with respect to the power source used to power the board or is there something else needed like a resistor ?

The short answer is no you don’t have to adjust the code to allow for different power sources.
There is likely to be some problem with the way you have all this wired up or how you are referencing voltages ( eg if you had a pot initially connected to 5v and now it’s connected to 9v) Can you provide a sketch for people to look at

I dont have a sketch but i do have some pictures.

This is showing how i have the encoder connected to the arduino, the lcd screen isnt currently connected because the problem doesn’t change with or without it.

The wires are as labeled in the pictures(all wires are from the encoder):

  • Green Wire: Signal A
  • White Wire: Signal B
  • Black near on Digital Ground: Ground
  • Black near on Power: Common
  • Red: 5V Power

The pictures have two different power sources, the black cord is the 9V Jack bolt from an outlet
The blue wire is from the computer and is a 5V source.

Once again the code i built above works correctly when connected to the computer but when plugged into the 9V it doesn’t.

image1.jpeg.png

image2.jpeg.png