How to adjust brightness of an RGB LED strip with Arduino Uno?

I have an RGB LED strip working with an Arduino Uno and would like to be able to control the brightness.

Below is my current working code and attached is diagram of setup.

Am using these MOSFET’s:

IRLB8721PbF (Datasheet)

LED strip specs are at end of post.

I’ve tried searches for:

“adjust rgb led strip brightness python serial”

“rgb led strip dimming python serial”

But any possible matches relate to implementations that are a bit over my head, or slightly off topic.

Python:

import serial

ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write(struct.pack('>3B', red_value, green_value, blue_value))

Sketch:

// digital output pin numbers
const int digitalOutputPinForRedLED = 9;
const int digitalOutputPinForGreenLED = 10;
const int digitalOutputPinForBlueLED = 11;

// global variables
int valueOfRed = 0;
int valueOfGreen = 0;
int valueOfBlue = 0;
int x = 1;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

  // set digital pin modes
  pinMode(digitalOutputPinForRedLED,OUTPUT);
  pinMode(digitalOutputPinForGreenLED,OUTPUT);
  pinMode(digitalOutputPinForBlueLED,OUTPUT);
}

void loop() {
    
  if (Serial.available()) {
    if (x==1) {
     valueOfRed = Serial.read();
     Serial.print("Red: ");
     Serial.println(valueOfRed);
     analogWrite(digitalOutputPinForRedLED, valueOfRed);
   }
    else if (x==2) {
     valueOfGreen = Serial.read();
     Serial.print("Green: ");
     Serial.println(valueOfGreen);
     analogWrite(digitalOutputPinForGreenLED, valueOfGreen);
   }
    else if (x==3) {
     valueOfBlue = Serial.read();
     Serial.print("Blue: ");
     Serial.println(valueOfBlue);
     analogWrite(digitalOutputPinForBlueLED, valueOfBlue);
   }
   x++;
 }
    else {
      x = 1; 
     }
delay(1);
}

Specs:

RGB LED strip

LED type: 5050
SMDColor: RGB
LED quantity: 300LED
Package: 5 Meter/REEL
Voltage: 12V DC
Working Power: 14.4W
Working temperature: -20° to 50°
Size: ⌀180mm
Length: 5m
Life Span: 50000+hours
Weight: 0.3kg

Why the Python? The Arduino runs on C++.

A bit rubbish that code but it should do something. Use the serial monitor to put single bytes to the read. Note you will be typing ASCII and the code is expecting a binary value, but it should give you something if your hardware is set up right.

Why do you have the Vin pin going to the 5V rail of the breadboard, which is powering the strip? Then you have some kind of supply going to the same 5V rail of the breadboard. And you have a 12V supply going into the barrel jack of the board.

Bad things are going to happen.

There is no 5V rail on the bread board it is just a rail.

Mind you the power for the strip will have to go through the reverse protection diode which has a limit of 1A but apart from that the circuit will work.