wrong PWM output value

hey guys have an annoying problemm with my code

int val = A0 analogWrite(2, val); analogWrite(3, val);

this code its a bit simple but you now what i mean i mapping my input val and im sending it as my output val to pin 2 and pin 3 on this setup my voltage on pin 2 is half so high as my pin 3 voltage ( pin 2 voltage 2,20 and pin 3 voltage ~ 4,3 4,5) if i change the output order to this

int val = A0 analogWrite(3, val); analogWrite(2, val);

my val on pin 3 its half so high as my pin 2 voltage normal i would don't mine but im controlling 2 DC motor with this, did some one have this same problem or im i doing something wrong?

A0 is the name of a pin. You need to read the analog value from that pin before writing it back out. Simply assigning the pin name isn't going to work.

You did not mention which Arduino you have. Different Arduinos have different PWM pins. Lets see, some of them are:

  • Uno: pins 5 and 6 always support PWM. Pins 3 and 11 support PWM if you do not use the tone library. Pins 9 and 10 support PWM if you do not use the servo library.
  • Leonardo: Like the UNO, but adds pin 13 as a PWM. I am not sure if the tone and servo libraries change PWM on pins 3, 9, 10, and 11.
  • Mega 2560 and Due: Pins 2-13 are PWM pins, but pins 11 and 12 do not support PWM if you have at least 12 servos.

So if you have an Uno or Leonardo, pin 2 would not be a PWM pin. Also pin 3 might not be a PWM pin if you use the tone library.

As gm1mfn notes, if you just assign A0, it will assign the pin number (14 for Uno, 18 for Leonardo, 54 for Mega 2560 and Due). I suspect you want to use:

int val = analogRead (A0);

MichaelMeissner: As gm1mfn notes, if you just assign A0, it will assign the pin number (14 for Uno, 18 for Leonardo, 54 for Mega 2560 and Due). I suspect you want to use:

int val = analogRead (A0);

Your next problem will be that analogRead returns a value between 0 and 1023, but analogWrite requires a value between 0 and 255. Therefore, depending on what is driving the analog input pin, you may need something like this:

int val = analogRead(A0);
analogWrite(2, val/4);
analogWrite(3, val/4);

dc42: Your next problem will be that analogRead returns a value between 0 and 1023, but analogWrite requires a value between 0 and 255. Therefore, depending on what is driving the analog input pin, you may need something like this:

Yes, I forgot about the difference in values between read and write. Thanks.

im using MEGA 2560 with an usb shield connected to a BT stick and im receiving an analog val in range 0 -255 and this mapping is simple, the PWM output is to control speed of my H-Bridges but like i mention the output voltage for first (define) pin is in range from 0- 2,2V and the second is from 0 to 4,4 4,5V can it be that arduino cant write 2 same val at the same time ?

can it be that arduino cant write 2 same val at the same time ?

The Arduino can't do two things (no matter what they are) at the same time. But, that has nothing to do with your problem.

and im receiving an analog val in range 0 -255

From what? What does the fact that there us a USB Shield or bluetooth stick have to do with that? You really need to explain what you are receiving the value from, and how you are receiving it. Reading from an analog pin and getting data from the serial port and getting data from the USB shield are three different things.

The better job you do of explaining exactly what is happening, the easier time we'll have of helping you.

im mapping one val to 2 output pins and my first define pin is all ways in range of 0 - 2,2v im receiving this val from an PS3 controller and on the serial print i have val from 0 to 255 so this can be the cause or maybe im wrong?

im mapping one val to 2 output pins

Still not going to post your code, I see. Well, I'll skip posting any answers, then.

sry for that here the code

#include <PS3BT.h>
#include <HBridge.h>
#include <usbhub.h>

USB Usb;
USBHub Hubl(&Usb);
BTD Btd(&Usb);
PS3BT PS3(&Btd,0x00, 0x09, 0xDD, 0x50, 0x8F,0xC6);

HBridge hb1(1);				//
HBridge hb2(2);				//
HBridge hb3(3);				//
HBridge hb4(4);				//

void setup()
{
	Serial.begin(115200);
	Usb.Init();
	if (Usb.Init() == -1)
	{
		Serial.print(F("\r\nOSC did not start"));
		while(1); //halt
	}
	Serial.print(F("\r\nPS3 Bluetooth Library Started"));

	hb1.setEnablePins(2,3);
	hb2.setEnablePins(50,29);
	hb3.setEnablePins(31,41);
	hb4.setEnablePins(53,43);

	hb1.setControlPins(42,44,46,48);
	hb2.setControlPins(52,23,25,27);
	hb3.setControlPins(33,35,37,39);
	hb4.setControlPins(45,47,49,51);
}

void loop()
{
	/*THE MANIN PROGRAM*/
	Usb.Task();
	if(PS3.PS3Connected)
	{
		//LedBlink();
		MotorOnePWM();
	}
}

/*Motor PWM*/
void MotorOnePWM()
{
	//Go Backward With L2
	if ((PS3.getAnalogButton(L2))&&!(PS3.getAnalogButton(R2))&&!(PS3.getButtonPress(CROSS)))
	{
		hb1.PWM(PS3.getAnalogButton(L2));
		hb1.movePWMBackward();
	}

	//Go Forward With R2
	if ((PS3.getAnalogButton(R2))&&!(PS3.getAnalogButton(L2))&&!(PS3.getButtonPress(CROSS)))
	{
		hb1.PWM(PS3.getAnalogButton(R2));
		hb1.movePWMForward();
	}

	//Rotate Conterclockwise L2+X
	if ((PS3.getAnalogButton(L2))&&!(PS3.getAnalogButton(R2))&&(PS3.getButtonPress(CROSS)))
	{
		hb1.PWM(PS3.getAnalogButton(L2));
		hb1.PWMturnLeft();
	}

	//Rotate Clockwise R2+X
	if ((PS3.getAnalogButton(R2))&&!(PS3.getAnalogButton(L2))&&(PS3.getButtonPress(CROSS)))
	{
		hb1.PWM(PS3.getAnalogButton(R2));
		hb1.PWMturnRight();
	}

	//Motor OFF
	hb1.Stop();
}

/*Led Blink if connected to controller*/
void LedBlink()
{
	digitalWrite(22, HIGH);
	delay(350);
	digitalWrite(22, LOW);
	delay(350);
}
    if ((PS3.getAnalogButton(L2))&&!(PS3.getAnalogButton(R2))&&!(PS3.getButtonPress(CROSS)))

Isyourspacebarbroken?

Links to the non-core libraries would be useful.

im mapping one val

What val(ue)?

to 2 output pins

Which 2 pins?

and my first define pin

I don't see any #define statements, so this makes no sense.

lib core

if ((PS3.getAnalogButton(L2))&&!(PS3.getAnalogButton(R2))&&!(PS3.getButtonPress(CROSS)))

Isyourspacebarbroken?

YES:D

im mapping one val

What val(ue)?

PS3.getAnalogButton(L2) its analog val in range from 0-255

to 2 output pins

Which 2 pins?

this hb1.setEnablePins(2,3); firs is for EN A and second is for ENB

and my first define pin

I don’t see any #define statements, so this makes no sense.

the #define pins are in .setEnablePins is for enable A or B and/or to set the PWM
the #define pins are in .setControlPins is for the enable inputs to control the rotary direction

lib core

Add it as an attachment, using the Additional Options button. I can't get to dropbox when at work, and don't particularly trust it when I'm not.

H-Bridges typically have enable pins that define direction. They are not required to be PWM pins. The speed is defined using other pins, which do need to be PWM pins.

how about a github link Arduino/libraries/HBridge at master · THXC/Arduino · GitHub

HBridge.cpp (5.18 KB)

HBridge.h (3.05 KB)

I'm using an L298 H bridge, and it's enable pins, when high, allow the directional pins to function or not, when low. I assume yours is the same. I'm also able to change the speed of two gear motors I'm driving by a PWM output on the enable pins. I'm using pins 6 and 11 on my UNO. Perhaps selecting other pins in your case might help.

I'm also not using the HBridge library. My method is more straight forward. I assign variables to the various pins and use the names in the write commands. You probably already know about doing that.

I also found that the L298 board I'm using does much better with a supply of 7 volts or more. You might check if you have enough voltage to drive your system.

Anyway, what your attempting is possible. Good luck.

im using a L298N hbridge i try different pins but the problem is the same i try with 7,2v battery pack but stile the same this is a screen of my problem voltage pin1 is corresponding with enable A voltage pin2 is corresponding with enable B the output voltage drops to 50% on that pin

i found my old project using the old fashion whey but i have the same problem but my output voltage thats enabling A drops to 75% so im starting assuming that this have something to do with this usb shild

dose any one now witch pins are reserved for the use of arduino USB shild?

Are you measuring the voltage on the PWM pins with them connected to the H bridge? If that's true, then disconnect them and measure without any load while the sketch is running. They should be the same. If not, then something is wrong with the Arduino board. Also disconnect the H bridge enable pins from the Arduino and measure the voltage on them while the chip has power to it. They should measure the same. If not, something is wrong with the H bridge.

i try that but the problem is the same im going to write a simple code to control my hbridge without any lib and will see if the problem still occurs

hey again i test my Mega PWM output voltage today and the val was 4,7 - 4,5
here the code its simple but was enough

int ledPin1 = 9;    // LED connected to digital pin 9
int ledPin2 = 3;
int fadeValue = 0;

void setup()  {
	// nothing happens in setup
	Serial.begin(115200);
	Serial.println("RDY");
}

void loop()  {
	// fade in from min to max in increments of 5 points:
	for(fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
		// sets the value (range from 0 to 255):
		Serial.print("+ ");
		analogWrite(ledPin1, fadeValue);
		analogWrite(ledPin2, fadeValue);
		Serial.println(fadeValue);
		// wait for 30 milliseconds to see the dimming effect
		delay(100);
	}

	// fade out from max to min in increments of 5 points:
	for(fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
		// sets the value (range from 0 to 255):
		Serial.print("- ");
		analogWrite(ledPin1, fadeValue);
		analogWrite(ledPin2, fadeValue);
		Serial.println(fadeValue);
		// wait for 30 milliseconds to see the dimming effect
		delay(100);
	}
}

so im assuming that the power difference now have something to do with arduino usb shild, can some one tell my witch pins use this shild?