Go Down

Topic: Help with my code! (Read 2591 times) previous topic - next topic


Hi good day! Im very much a newbie in Arduino and I have an assignment.  =( I made some few codes but when i tried it on, it didn't worked properly. Can somebody help me construct the codes? Its about controlling the brightness of the led using potentiometer but in a different manner since I'm using a transistor.

Here are the parameters:

The circuit must have a potentiometer to control the brightness, 4 transistors acting as a switch, 2 leds connected in 200 ohms resistors.

The algorithm must be like this:

When I'll turn the knob of the potentiometer, the brightness of the led must adjust also whether increase or decrease the brightness but at the same time the two led must be blinking alternately. This is just a test for my single phase ac motor speed control assignment.

Here is the diagram:

The principle must be like this. Q1 and Q4 must be paired. Q3 and Q2 must be paired. Instead of using a motor just like the image above, im using a led connected in resistor instead.

The principle is just like this also:


James C4S

And the code you wrote is where?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com


Hi Squared, looks to me like you are using a sort of H bridge circuit, firstly are you trying to create a two way motor, also if you want to control the speed of the motor and that motor is brushless then you probably need to use PWM (pulse width modulation), if you are using a brushed motor, then you will need to use the analog outputs, and then add some amplifier circuitry, also is your motor AC Inductive, of DC Synchronous, all can be found in the spec sheet.

Firstly you will need to connect your wiper to one of he analog inputs, then read that into a variable using analogRead(), this will give you a value of 0 - 1023, (0 = 0V, 1023 = 5V), after you have the value you must decide what you are going to do with it. That depends on the motor you have, you can ether increase the duty cycle of the PWM or you can increase the DV voltage 0-5V of another analog pin then take that to an amplifier and then to the motor.

First of all find what type of motor you are using also do your really need the led's to flash? 


Hi. Actually, my assignment is to control the induction motor's speed. A 3 phase induction motor. I have read about PWM and AnalogRead and topics related to it but before going to the real  stuff, before testing the 3 phase induction motor, can I just test it first using led connected in delta or wye? Which is connected to 6 transistors that act as a switch? Do you have any code or topic related to my problem mate?  =(


Jan 20, 2012, 09:16 am Last Edit: Jan 20, 2012, 10:12 am by AWOL Reason: 1
This will let you get some basic control of the motor, or LED brightness to indicate the motor speed, but do you want a switch to change the direction? if so then i think you might need a sensor in there to see where the coil is in relation to the magnet.

Code: [Select]

// connect your motor to pin 5, but not directly, maybe though a transistor, and you might need a voltage devider!

//Connect the pot to analog pin 3


unsigned long WiperPosition = 0;  // the position of the pot returned as a value from 0 to 1023

int WiperPin = 3; // connect the pot to analog pin 3

int MotorPin = 5;

unsigned long DutyCycle = 0;

void setup()

void loop()
 DutyCycle = WiperPosition/4;
 WiperPosition = analogRead(WiperPin); // Read the analog value of the pot, 0-1024 or 0v-5v
 analogWrite(MotorPin, DutyCycle);


Moderator edit: Code box, please! (BTW, pinMode isn't necessary for a pin used solely for analogWrite)


^^ thank you so much. Can you please explain this line to me?

DutyCycle = WiperPosition/4;

why is it divided by 4?


why is it divided by 4?

Because the ten bit analogue to digital converter returns a value between 0 and 1023, but the PWM generated by analogWrite is eight bit, so a value between 0 and 255
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


thx, wasn't sure how to put a code box, and pin understood!


^^ Thank you so much for all the explanations. Im sorry, im really a newbie but im reading helpful ebooks everyday regarding arduino.

Anyways, can you help me do this? Lets say for example i have a single phase ac motor and I need to control its speed using potentiometer. I'll be using 4 transistors. Transistor A, Transistor B, Transistor C and Transistor D. I want Transistor A and C to be paired and be synchronized and i want Transistor B and D to be paired and synchronized also when i turned the potentiometer's knob . Should I use the AND (&) function? I mean do you have any codes or tutorials which is related to this?  =(


You circuit diagram above shows a mains voltage motor - are you quite sure you know what you're doing here?
You say "assignment" - do your supervisors realise what you're doing?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131