LCD Code Please

Hello,

I am in a need of help i have a project that consist of controlling a DC motor speed with a potentiometer and a L298D . I made it only that far, now i’m stuck to how to add a display that shows the speed . I have no idea how to write it’s code nor how to wire it.

Please i really need help

My email: edited.edited@edited.com

Thank You guys

Circuit diagram.jpg

code.png

Moderator: Email edited

Do you mean the actual speed in rpm, or the value of your variable "motor_speed".

In either case you need to give details of your LCD: they aren't all the same, and imo the easiest to use is an I2C version. It's easy once you install the appropriate library and look at the library's examples of how to display a variable.

If you need the actual speed in rpm, that's more complicated and you will need a sensor like an encoder or a Hall effect sensor.

Thank you for replying 426hemi. I'm looking to display the variable "motor_speed" not the speed in rpm.

And for the LCD the I2C is the one i'm using.

Could you help me with the code please? This is my first time arduino project.

Thank you

The code below is the HelloWorld example that comes with the I2C LCD library, or at least the library I use.

Instead of the line:

lcd.print("Hello, world!");

use something like:

lcd.print(motor_speed); //with no quotes

at the appropriate point in your loop().

And of course stick all the lines from above setup() into your sketch, as well as the begin() in setup().

(Of course you might have a different library.....)

Thank you, but i have no idea what to write. As i stated before it's my first time programming with arduino.

Hello,

I am in a need of help i have a project that consist of controlling a DC motor speed with a potentiometer and a L298D . I made it only that far, now i’m stuck to how to add a display that shows the speed . I have no idea how to write it’s code nor how to wire it. Can some one please write me the code?

Please i really need help

// Arduino DC motor speed and direction control

#define button 8
#define pot 0
#define pwm1 9
#define pwm2 10

boolean motor_dir = 0;
int motor_speed;

void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
}

void loop() {
motor_speed = analogRead(pot) / 4;
if(motor_dir)
analogWrite(pwm1, motor_speed);
else
analogWrite(pwm2, motor_speed);
if(!digitalRead(button)){ // If direction button is pressed
while(!digitalRead(button)); // Wait until direction button released
motor_dir = !motor_dir; // Toggle direction variable
if(motor_dir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
}
}

The easiest way would be to just print the speed out to the Serial Monitor as a start. To do that, you need to use the Serial library. There are examples in the IDE.

After you accomplish that, you will need to connect some sort of display. The easiest are the displays that use the i2c protocol (two wire or TWI) which you can purchase and install very easily. They take care of driving all the pins required to control a display. You will use commands very similar to the Serial library to write to the display, just like writing to the serial monitor.

Please i really need help

Then post your code.

Please display your image(s) in your post so we can see it(them) without downloading it(them). See this Simple Image Guide

Please do NOT post a picture of code. Just post it as text.

And to make it easy for people to help you please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Your title does not let people know what you need help with. If you edit your Original Post you can change it. To my mind something like “Help to display RPM on a display” or “Help choose a display to show RPM” would be more suitable. - I’m not sure more accurately describes your need.

…R

here is the code. Sorry guys

// Arduino DC motor speed and direction control

#define button 8
#define pot 0
#define pwm1 9
#define pwm2 10

boolean motor_dir = 0;
int motor_speed;

void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
}

void loop() {
motor_speed = analogRead(pot) / 4;
if(motor_dir)
analogWrite(pwm1, motor_speed);
else
analogWrite(pwm2, motor_speed);
if(!digitalRead(button)){ // If direction button is pressed
while(!digitalRead(button)); // Wait until direction button released
motor_dir = !motor_dir; // Toggle direction variable
if(motor_dir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
}
}

Read the 'How to Post' sticky at the top of each section.

Hello,

I am in a need of help i have a project that consist of controlling a DC motor speed with a potentiometer and a L298D . I made it only that far, now i’m stuck to how to add a display that shows the speed . I have no idea how to write it’s code.Please i really need help.

The code:

// Arduino DC motor speed and direction control
 
#define button   8
#define pot      0
#define pwm1     9
#define pwm2    10
 
boolean motor_dir = 0;
int motor_speed;
 
void setup() {
  pinMode(button, INPUT_PULLUP);
  pinMode(pwm1,   OUTPUT);
  pinMode(pwm2,   OUTPUT);
}
 
void loop() {
  motor_speed = analogRead(pot) / 4;
  if(motor_dir)
    analogWrite(pwm1, motor_speed);
  else
    analogWrite(pwm2, motor_speed);
  if(!digitalRead(button)){                // If direction button is pressed
    while(!digitalRead(button));           // Wait until direction button released
    motor_dir = !motor_dir;                // Toggle direction variable
    if(motor_dir)
      digitalWrite(pwm2, 0);
    else
      digitalWrite(pwm1, 0);
  }
}

now i'm stuck to how to add a display that shows the speed . I have no idea how to write it's code.

Don't you suppose that the code might be different for each kind of display?

now i'm stuck to how to add a display

What type of display do you have or are you intending to get ?

You could always start by displaying the speed in the Serial monitor.

“What display?” is the million dollar question :wink:

And some tips:

  • Use the enable pin for PWM and connect IN1 and IN2 to digital outputs. Saves a PWM pin and you get nicer motor movement and less heat.

  • DON’T call something what it does on the Arduino, call it something useful like what you do with it. Aka, not ‘pwm1’ but ‘motor_in1_pin’ (or more common in the Arduino world: '. ‘motorIn1Pin’)

  • And once you start numbering variables, most of the time it’s time to use arrays. Although here it’s not to bad.

  • Use something smarter for the button with proper debounce etc. I like the Bounce2 library for that.

  • If you refer to an analog pin, use ‘A0’ instead of ‘0’. That way you can never confuse digital pin 0 and analog pin 0 with each other.

  • DON’T use macros (#define) for pins. Yes, a lot of tutorials do and yes, it works. But C++ (which the Arduino IDE uses) has a much nicer alternative and that’s the ‘const’ keyword. In case something goes wrong, the compiler will output wayyyyyy more readable errors :wink:

const byte ButtonPin = 8;
const byte PotPin = A0;
const byte MotorIn1Pin = 9;
const byte MotorIn2Pin = 10;

True but i want to display on a I2C display the percentage of the Motor speed since i'm changing speed with the potentiometer.

Wow. You pretty much barfed all over the forum. Please stop doing that.

@accountsays, do not cross-post. Threads merged. Ditto.

[quote author=Coding Badly link=msg=3721750 date=1525680734]
You pretty much barfed all over the forum.[/quote]
Holy! Indeed!

@accountsays, I2C is just a bus, not a type of display. It's a bit like saying you drive petrol when we ask what car you drive. Post at least a link to the screen you bought.