Taking speed sensor data for the motor wheel (electrical circuit)

Hello there! I just wanted to make sure that my electrical circuit is correct. The project is working, but I want to be 100% sure it’s just right. Here is the code for this scheme:

#include <GyverTM1637.h>

#define PWM          9
#define IN1          12
#define IN2          4

#define SPEED_SENSOR 7

GyverTM1637 disp(2, 3);

double wheelRadius      = 0.325;
volatile int pulseCount = 0;
unsigned long lastTime  = 0;
double speed            = 0;
int pwmCounter       = 0;

char currentKey                 = 0;
unsigned long lastRepeatTime    = 0;
const unsigned long repeatDelay = 100;
const unsigned long repeatRate  = 100;  

void setup() {

    Serial.begin(9600);
    Serial1.begin(9600);

    disp.clear();     
    disp.brightness(7);
    disp.point(1);

    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(PWM, OUTPUT);

    pinMode(SPEED_SENSOR, INPUT);
    attachInterrupt(digitalPinToInterrupt(SPEED_SENSOR), countPulses, RISING);

}

void countPulses() {

  pulseCount++;

}

void processKey(char c) {

    if (c != '0' && c != '#') {

        if (c == 'w') { //Up Key (Blueduino App, Joystick)

            if (pwmCounter >= 0 && pwmCounter < 255) {

                pwmCounter++;
                analogWrite(PWM, pwmCounter);
                digitalWrite(IN1, 0); 
                digitalWrite(IN2, 1); 

            }

        } else if (c == 's') { //Down Key (Blueduino App, Joystick)

            if (pwmCounter >= 1 && pwmCounter <= 255) {

                pwmCounter--;
                analogWrite(PWM, pwmCounter);
                digitalWrite(IN1, 0); 
                digitalWrite(IN2, 1); 

            }

        }

    }

}

void loop() {

    if (millis() - lastTime >= 1000) {

        detachInterrupt(digitalPinToInterrupt(SPEED_SENSOR));
        speed = (pulseCount / 20.0) * wheelRadius * 3.6;
        pulseCount = 0;
        attachInterrupt(digitalPinToInterrupt(SPEED_SENSOR), countPulses, RISING);
        lastTime = millis();

    } else {

        if (Serial1.available() > 0) {

            currentKey = Serial1.read();
            lastRepeatTime = millis();
            processKey(currentKey);

        }

        if (currentKey != 0 && (millis() - lastRepeatTime) > repeatDelay) {

            if ((millis() - lastRepeatTime) % repeatRate == 0) {

                processKey(currentKey);

            }

        }

        disp.displayClock(speed, (speed - (int)speed) * 100);

    }

}

Any other advice or imprоvements or critics both on the schematic or code would be much appreciated.

Very hard to look at code that is mostly vertical white space. Also, it needs to be Auto Formatted. That is under the Tools menu.

6V is a really low voltage for that H-bridge (L298).

1 Like

That is a nice diagram, but how do we know that is actually what you did. Either a lot of overhead photos so we can see both ends of each wire, or easier is draw by hand the actual wires and post that.
If it's working, whay do you expect for feedback?

build_1971, yes, thank you! I reality I used a 8 x AA battery pack. Just forgot to mention it. I have fixed the scheme in the post.

sonofcy, I was just afraid of a critical issue about this project.

Can I ask the purpose of the detachInterrupt followed by attachInterrupt???

You might want to look at a modern controller, the L298 is often used as a room heater. The Polulu site has a lot of good data. Here is the google answer.

The best, most efficient, and modern replacements for the L298N dual H-bridge motor driver are MOSFET-based, featuring higher efficiency (90%+) and no need for large heatsinks. Top replacements include the TB6612FNG (1.2A, 3.2A peak) for small robotsDRV8833 (1.5A–2A) for low-voltage applications, and VNH2SP30 (30A) for high-power demands.

Pololu Forum +4

I often use the TB6612FNG

Welcome Back!
I expect you will have problems. Typicall AAA batteries will look like this:
Alkaline discharge curve is sloped, not flat.
Typical pattern:
1.60 V fresh
1.50 V quickly after light use
1.40 V mid-life
1.30 V usable
1.20 V near end
1.10 V weak
1.00 V mostly done
The L298 will drop 3 volts leaving you 5V on the motor at end of battery life. The PP9 is a bad choice to power the Arduino, it will discharge very quickly.

Looking at your picture it will not work and go through batteries in short order, they have there outputs shorted together.

The 9V will not be supplying enough to properly power the Arduino Mega over time. The battery will start at about 9.8 and drop to about 6.5 when discharged. The minimum voltage for Vin is 7V, that is reached when the battery is about 75% used up.

Your pictures do not pass as an annotated schematic, the language of electronics, and is very hard to follow. It requires a fair amount of knowledge about the parts. There is no indication of what parts you used. These wiring diagrams assume the builder has the exact same parts.

Overall you did a good job. but you asked.

1 Like

sonofcy, the code for the sensor to take data from the wheel was made with artificial intelligence as I needed some Software Repeat Implementation for the buttons inside the app which managed the motor, so I never ran into the details much. Actually, I do not know exactly how the sensor is managed with the C++ code. The most important thing is the electrical scheme, the code seems to work.

In the future please tell us if the code was created by AI. Many folks here will not help with AI code, some will. Good luck.

How do you know this?

gilshultz, thank you for your reply! The parts on the scheme are just those ones that I used:
Arduino Leonardo
TM1637 Display
8 x AA Battery Pack
Speed Sensor Like FC-03LM393
TT Motor Reductor Arduino
Bluetooth HC-05
Motor Driver L298N
Arduino Mini Breadboard
Arduino (DC Barrel Jack)
Encoder Wheel
Krona 9V

I built it in reality twice. The speed sensor shows 0 - 7 km/h against 0-255 PWM. Of course, I need more being sure in data precision to make it more practical.

Ok. Do you still have a question?

sonofcy, no, thank you. I just wanted to be sure that what I have been doing is correct. Yesterday, I forgot about additional ground for this scheme and had to rebuild it quite much.

You are good to go as long as you followed the advice given particularly in post 3 and 8

You missed the links: for example the Battery, possibly a PP3:
Krona produces several types of 9V batteries, including alkaline, lithium, and rechargeable variants. The most common types are marked with designations like 6F22, 1604, and 6LR61, among others.

At this point with the lack of information, I have other things to do with mi time! At this point I will simply say Good Luck!