Skip to content

Inverted Pendulum Project

The inverted pendulum – an instructional project on the design of autonomous system.

The goal of this project and report is to provide the beginner with the instructions to practically realize the design of an inverted pendulum. The following objectives have been realized and are reported here:

1.       To Plan, design and build an inverted pendulum.

2.       To use the Proportional, Integral, and Derivative method to achieve stable control

3.       To provide an instructional guide for anyone looking to build an inverted pendulum using PID control

4.       To create a Bill Of Materials

5.       To provide CAD Models, assembly and tuning instruction, and a cost estimate for the project.

6.       To perform structural Finite Element Analysis to analyze the design.

7.       To provide photographs, video, and supplementary information for further guidance.

Introduction

The objective of this project was to design, implement, and learn about the control of an Inverted Pendulum.  I chose an inverted pendulum because it represents a one degree of freedom model of control of an inherently unstable system.  While there are many different control schemes such as Dead beat control, H-infinity methods which can be used to provide control over a system, PID control is widely used and there are many resources available making it an ideal choice for an initial controls project.

PID control consists of three different parameters, the Proportional, Integral, and Derivative terms.  The proportional term is directly proportional to the current measured error.  The integral term determines the reaction based on the sum, or integral, of recent errors.  Lastly, the derivative term is determined based on the rate at which the error is changing.  The three of these terms summed together can be set to obtain desired system characteristics such as settling time and rise time of the system about its set point.

The inverted pendulum control system I designed has several key components.  As with most control system, the components can be classified into several groups:

Figure 1 – The Plant

Plant:

a.       The plant is the system to be controlled.

b.      This can be the temperature of a room, the speed of a car, or in this case, the angle of the pendulum.

Set Point (Reference Input)

a.       The set point is the desired value that you want the control system achieve.

b.      For this project the set point is 90 degrees relative to horizontal.

Figure 2 – Arduino Mega Microprocessor

Controller (shown as the circle which adds or subtracts the feedback signal with the Set Point)

a.       The controller reads the output of the plant and compares it with the set point and decides what action to take.

b.      The Arduino microprocessor is used as the controller for this project.

Figure 3 – Geared Electric Motor

Actuator (Control Elements)

a.       The Actuator implements the correction provided by the controller.

b.      An electric motor is used to move the cart which will affect the angle of the pendulum.

Figure 4 – Heavy Duty Potentiometer

Feedback Elements (Sensor)

a.       The Sensor measures the output from the plant

b.      A potentiometer is used to measure the angle of the inverted pendulum.

Design Philosophy:

The cart was designed to be as simple as possible to facilitate ease of fabrication and assembly.  I have also tried to minimize the number of screws and bolts necessary by having multiple components attached using the same fasteners.  By considering maintenance and design for manufacturing, modification and assembly is made simple by ensuring that all components can be easily put together or removed with the fewest number of operations.  Hardware components are off the shelf and can be found at a local hardware store, and the electronic components are readily ordered online from a number of electronics specialty stores.

The chassis and several of the non-metal components were cut from a single sheet of acrylic that was 0.22” thick.  The size was kept small so that the cart would be lightweight but still retain structural rigidity.

Figure 5  – The Chassis

Other components consisted of right angle extruded aluminum pieces which were also purchased in longer lengths were cut to size and drilled as needed using a drill press.

Figure 6 – Pendulum Holder

Figure 7 – Potentiometer Holder

Components which are not available from the local hardware store such as the Arduino Microprocessor (Figure 2), the geared electric motor (Figure 3), the motor controller (Figure 8 below), and the potentiometer (Figure 4) were ordered from electronic specialty hobby stores such as www.sparkfun.com , and www.pololu.com.  These components represent the majority of the cost and most complex components of this project, however as they are high quality components they can be re-used for future projects.

Figure 8 – Heavy Duty Motor Controller

Figure 9 – The actual assembly

Analysis of Cart Design:

To ensure the cart will function without exceeding the material specifications, I have employed the use of SolidWorks Simulation Finite Element Analysis.  The first analysis that I ran was a basic static linear analysis to ensure that the free-standing pendulum would not yield or deform excessively under gravity loading.

The first step in most FEA analysis is to simplify the model for analysis:

Figure 10 – Simplified for FEA Assembly

Starting with the full assembly shown in Figure 1, I have simplified the assembly substantially making full use of various FEA techniques.  For instance, components which have a length to thickness ratio of 20:1 or greater should be modeled using 2-D shell elements rather than the standard 3-D tetrahedral element types that Simulation uses for solid bodies by default.  There are several advantages to using 2-d shell elements with a virtual “thickness” over 3-d tetrahedral elements; the main advantage is that there are fewer nodes and subsequently degrees of freedom which need to have their displacements calculated.  By doing this the time to solve is greatly reduced.

Another tool which substantially reduces solve time is removal of fasteners such as bolts, rivets, screws, pins etc from the model and replacing them with their equivalent simulation connectors.  I have several machine screws and nuts in my assembly which I removed and replaced with Bolt Connectors in Simulation.  The bolt connector creates a rigid link between the top and bottom components in which you can specify the head and nut diameter, the bolt torque or axial preload, friction, and a whole host of other bolt parameters all designed to reduce solve time.

Similar to the reasoning behind using 2-d shell elements to reduce the number of elements in the FEA analysis (high length to thickness ratio), another trick is to replace the pendulum rod itself with a “remote mass”.  This tool will replace a component with a virtual representation of its mass located at the center of gravity of the component and connect it to its corresponding geometry using rigid “bars” eliminating the necessity to discretize the geometry with elements.  In this case, I replaced the pendulum rod itself with a “remote mass”.

While the cart is connected to my desk in the real world, the FEA model must be restrained in virtual space to allow the forces applied to reach equilibrium.  This task falls to fixtures in FEA, and in my analysis consists of a “Virtual Wall” applied to the wheel hub, and two “fixed” restraints which fix the cart and the track.  The “virtual wall” represents how the wheel would sit on the table and only provides a reaction force opposite to any load applied so long as it does not exceed the force of friction defined.  I have used both 2-D shell and 3-D solid elements in this analysis which have 6 and 3 degrees of freedom respectively.  The fixed geometries are used to restrain the components by all 6 degrees of freedom to prevent rigid body motion.  If rigid body motion is not sufficiently restrained in static analysis, the stiffness matrix would no longer be valid and the study would not converge on a solution.

Lastly, because this is an assembly of many individual components, the interaction between these components has to be defined.  SolidWorks Simulation tries to simplify this for users by applying a “Global bonded contact” by default.  What this means is that for any two components which are initially touching, they will be considered “glued”, “welded”, or “bonded” together.  The problem with this definition is that if two components are welded together, they will be able to transfer both forces and moments.  While this may be valid for some interactions between parts, they are not representative of many connections.

I have manually defined “no penetration” contact sets between many of the components such as ones which are held together using a bolt connector, as a “bonded” contact set would be inappropriate for the interaction between these components.  Other component interactions such as the one between the potentiometer and the pendulum rod which is a press fit in the real world I have defined as a bonded contact set.

Below are the results of the FEA analyses I have run:

Static Study Results

Name Type Min Max
Stress1 VON: von Mises Stress 20.2984 N/m^2Node: 48029 5.0278e+007 N/m^2Node: 59615
cart-2a-final-Study 5-Stress-Stress1

While the maximum stress for my analysis shows a Von Mises stress in excess of 50 Mpa, the maximum stress is found in a region which is associated with a bolt connector.  Due to the nature of FEA analysis, stress values found near boundary conditions such as fixtures and connectors are inaccurate and need to be ignored.  The reason for the stress concentrations has to do with how the bolt connector is physically modeled in FEA.  When a bolt connector is defined it is created using a series of “rigid” links connecting the top of geometry with the bottom of its corresponding geometry.  These rigid links have infinite rigidity and do not deform regardless of the loading applied to them.  The geometry itself is not rigid and does deform as defined by its material properties.  The connection of infinitely rigid connectors pressing down on a deformable material has the effect of generating false stress concentrations in regions close to them.  Alternatively you can model the bolts and hardware to include them in your analysis if the stresses in regions nearby or the connectors themselves are of interest to you.

Instead if we utilize a result analysis tool called iso-clipping, we find that the percentage of elements with a stress value over 2 MPa is less than 2%, or the elements which are located next to boundary conditions, indicating that the stress values in areas of interest are well within material property limits.

Name Type Min Max
Displacement1 URES: Resultant Displacement 0 mmNode: 59001 0.0447337 mmNode: 58257
cart-2a-final-Study 5-Displacement-Displacement1

The displacement results shown here reflect a maximum displacement of 0.0447 mm found in the pendulum holder.  This amount of deflection is well within acceptable values and should not cause interference between components in the actual assembly.

For more information about the FEA method, see Appendix A.

Modal Analysis:

Because the cart is not a static system in the real world, a static study is not sufficient to fully characterize how its real world behavior would be and whether the design is sufficiently robust.  I have also run a frequency, or modal analysis to determine the natural modes of the assembly.  The natural modes are important because at certain excitation frequencies, there can be resonance which can cause severe vibration and can damage the assembly.

Following the same general setup as in the static study in terms of simplification, connectors, and fixtures the results of the frequency study of the first  5 natural modes are as follows:

Frequency Study Results

Name Type Min Max
Displacement1 URES: Resultant Displacement Plot for Mode Shape: 1(Value = 3.58878 Hz) 0 mmNode: 10635 6626.81 mmNode: 11478
cart-2a-final-Study 4-Displacement-Displacement1

Name Type Min Max
Displacement2 URES: Resultant Displacement Plot for Mode Shape: 2(Value = 4.71398 Hz) 0 mmNode: 10635 6938.83 mmNode: 11478
cart-2a-final-Study 4-Displacement-Displacement2

Name Type Min Max
Displacement3 URES: Resultant Displacement Plot for Mode Shape: 3(Value = 26.241 Hz) 0 mmNode: 10635 6929.69 mmNode: 11476
cart-2a-final-Study 4-Displacement-Displacement3

Name Type Min Max
Displacement4 URES: Resultant Displacement Plot for Mode Shape: 4(Value = 29.6325 Hz) 0 mmNode: 10635 6924.85 mmNode: 11476
cart-2a-final-Study 4-Displacement-Displacement4

Name Type Min Max
Displacement5 URES: Resultant Displacement Plot for Mode Shape: 5(Value = 77.5573 Hz) 0 mmNode: 10635 6979.64 mmNode: 11478
cart-2a-final-Study 4-Displacement-Displacement5

Mode List

Frequency Number Rad/sec Hertz Seconds
1 22.549 3.5888 0.27865
2 29.619 4.714 0.21214
3 164.88 26.241 0.038108
4 186.19 29.633 0.033747
5 487.31 77.557 0.012894

 

The results plots here show the shapes of the first 5 natural modes and while there are displacements listed, they are not the actual displacements of the assembly, and only represent proportional displacement with respect to the nodes in the study.  The modes are listed in a table format above.

The final FEA study I performed on this assembly was a linear dynamic shock test.  I have subjected the cart to an impulse shock testing to represent the motor turning on and off in rapid succession.  Dynamic loading can be many orders of magnitude more than static loading and this analysis will help determine whether any components will fail due to impulse loads.

Impulse Study Results

Name Type Min Max
Stress1 VON: von Mises Stress at Step No: 20(0.1 Seconds) 0 N/m^2Node: 102781 1.94796e+006 N/m^2Node: 120673
cart-2a-final-Study 3-Stress-Stress1

Name Type Min Max
Displacement1 URES: Resultant Displacement at Step No: 20(0.1 Seconds) 0 mmNode: 41609 0.913942 mmNode: 49250
cart-2a-final-Study 3-Displacement-Displacement1

The results of impulse loading are actually related to the frequency (modal) analysis.  In fact prior to running an impulse study, the frequency analysis is automatically run to determine the natural modes and depending on the excitation (impulse, modal, or random vibration), the results will depend on what the natural modes are.  In this case, the impulse loading I applied was a displacement of 0.1mm over 0.005 seconds.  This caused a maximum stress of approximately 2 MPa in the link between the cart and the pendulum which is well within material specifications.

Fabrication and Assembly Instructions:

Following my design philosophy of trying to keep the complexity and cost of this project to a minimum the tools required to fabricate this assembly are fairly simple.  Utilizing a jig saw and a drill press should be sufficient to create this assembly.  A table saw with a plastic and metal blade, or a hand held drill can be substituted with some difficulty.  As a precaution, I would highly recommend wearing safety goggles and at least a pair of work man leather gloves as well as a pair of close toed shoes, steel toed boots are preferable, however.

I have included drawing files of the components to be machined.  They can be printed in a 1:1 scale and taped to the components as a template to help approximate where to drill and where to cut the components.  Prior to drilling I would recommend the use of a guide dimple which can be applied using a metal punch and a hammer.  The guide dimple is a depression created on the surface to be drilled so that the drill bit stays centered.  You can also use a guide hole with a smaller drill bit ensuring accurate hole placement when you go to drill the full sized hole.

Assembly is fairly simple once the components have been fabricated and the ordered components have arrived.  The steps are described below:

Figure 11 – Assembly Step 1

Figure 12

Figure 13 – Assemble the motor mount bracket and the potentiometer holder

Figure 14  – Place the Pendulum holder in place and screw the nuts down

Figure 15

Figure 16 – Insert the Potentiometer into the holder and screw it down

Figure 17 – Screw down the Potentiometer

Figure 18

Figure 19 – Attach the motor to the bracket using the two #2 machine screws

Figure 20

Figure 21

Figure 22 – Put the wheel moto hub onto the shaft of the geared motor

Figure 23 – Insert the set screw into the side of the hub and tighten

Figure 24 – Assemble the pendulum holder

Figure 25

Figure 26 – Insert the Pendulum, this may be a tight fit, however it should be tight

Figure 27 – Press fit the pendulum assembly onto the potentiometer

Figure 28

Figure 29 – Screw the wheel onto the hub

Figure 30

Figure 31 – Slide the cart onto the rail and add the end holders

The Code:

This program was written in the Writing programming language, an open source language which the arduino uses.  Tuning was done by slowly increasing the P gain (kp), until the system was oscillatory, and then the ki and kd gains were increased until critical damping was achieved.  You can also add in some other sensors for positioning of the cart with the appropriate code.  If you increase the gain in the direction opposite that the pendulum is going, you can force the cart back to it’s original neutral position.

#define INA 53
#define INB 51
#define PWM2 13
#define POT 0

int errorcw = 0;
int errorccw = 0;
int correction = 0;
double kp = 57;
double ki = 26;
double kd = 12;
int PID = 0;
int error = 0;
int last_error = 0;
int angle = 0;
int sp = 839;

void setup()
{
Serial.begin(9600);
pinMode(INA, OUTPUT);
pinMode(INB, OUTPUT);
pinMode(PWM2, OUTPUT);
pinMode(POT, INPUT);

}

void loop()
{
char buffer[5];
angle = analogRead(POT);
Serial.print(“#S|PIDLOG|[“);
Serial.print(itoa((angle), buffer, 10));
Serial.println(“]#”);
if(angle < sp)
{
errorcw = (sp-angle);
correction = calcPid(errorcw);
analogWrite(PWM2, correction);
digitalWrite(INA, LOW);
digitalWrite(INB, HIGH);
}
if(angle > sp)
{
errorccw = (angle-sp);
correction = calcPid(errorccw);

analogWrite(PWM2, correction);
digitalWrite(INA, HIGH);
digitalWrite(INB, LOW);
}
//  delay(50);
return;
}

int calcPid(int error)
{
PID = (kp*(error)) + (ki*(error + last_error)) + (kd*(error – last_error));
last_error = error;

return constrain(PID, 0, 255);
}

Cost of the project:

Another objective of this project was to build the inverted pendulum within a reasonable budget while implementing full PID feedback control and minimizing the resources necessary to build the inverted pendulum.  The design should be easily fabricated and assembled with basic tools.

Most of the hardware was sourced from the local Orchard Supply Hardware and should be available from any major hardware store.  The specialized components such as the geared motor, wheel, motor driver, and Arduino Microprocessor were purchased online from specialty stores such as: www.sparkfun.com , and www.pololu.com .

The Bill Of Materials is as follows:

ITEM NO. PART NUMBER QTY. Cost
1 Cart_Chassis 1
2 Polou_Gear_Motor_Bracket 1 3.48
3 Polou_Gear_Motor 1 20
4 pot 1 0.95
5 Pot_Holder 1
6 rod 1
7 cart_track_holder 1
8 Mirrorcart_track_holder 1
9 Polou_Gear_Motor_Bracket_HUB 1 3.48
10 Outer_Track_Holder 2
11 rail 1 6.98
12 Chassis_Rail_Spacer 1
13 MirrorChassis_Rail_Spacer 1
14 CR-PHMS 0.112-40×0.625×0.625-N 2 0.03
15 MSHXNUT 0.112-40-S-N 2 0.01
16 Polou_90mm_WHEEL 1 4.98
17 Ground_Spacer 1
18 Ground_Spacer_2 1
19 Ground_Spacer_3 1
20 CR-PHMS 0.086-56×0.4375×0.4375-N 2 0.02
21 CR-PHMS 0.138-32×0.625×0.625-N 4 0.04
22 MSHXNUT 0.138-32-S-N 4 0.03
23 Pendulum_Holder 1
24 CR-PHMS 0.099-48×0.375×0.375-N 2 0.01
25 MSHXNUT 0.099-48-S-N 2 0.01
26 Pendulum 1 5.89

Appendix A

Static FEA analysis:

When a static FEA analysis is performed, the first thing that is calculated is the nodal displacements.  Because these are calculated first and everything else is calculated from the nodal displacements, the displacement results are generally more accurate than the stress results for a given mesh.  Stress is equal to the average force over an area and the more elements with nodes that exist over an area, the more accurate the average stress calculation will be.

Calculations of the stresses in each element are computed at special “Gauss Points” which are the optimal sampling points for stress computation in each element.  Since nodes are often shared by multiple elements, the nodal stress values reported are the average of the local gauss points in the vicinity of that particular node.

When the results for stress calculations at gauss points are performed, the results can be averaged over the element itself or from all gauss points closest to a node.  This results in two different types of average stress plots, an averaged nodal stress plot, and an averaged elemental stress plot.  The nodal stress plot is usually of most value, whereas the elemental stress plot is often used to check for problems with the mesh or stress results.  The element stress plot can be helpful to check for large step changes in reported stress values as the whole element will generally be displayed as a different color from the rest of the surrounding elements.

Calculation of the nodal displacements is generally far more accurate than that for stresses with an unrefined mesh.  In general simulation likes to have at least two tetrahedral elements across the thickness of the thinnest geometry in an analysis to guarantee relatively accurate stress results, whereas if you are only interested in displacements, often a single layer of 3-d tetrahedral elements are necessary.

Files:

zip file containing solidworks files and arduino code.


Advertisements
43 Comments
  1. Dexter permalink

    Hi Andy.
    IM trying to use your code for the inverse pendulum and it is highlighting the
    int PID = (kp*(error)) + (ki*(error + last_error)) + (kd*(error – last_error));

    line and giving the error

    stray’\’ in program

    can you help me out
    THANKS!

    • Hey Dexter,
      Are you using the exact code that I wrote? If so there shouldn’t be any errors, as this is what I ran, can you post what code you’re using? It sounds like a syntax error.

      -Andrew

  2. Dexter permalink

    I tried to use the same code that you had and it would not work. I am using a arduino UNO and the arduino motor shield if that makes a difference… here is the code i am trying to use

    #define INA 13
    #define INB 12
    #define PWM1 3
    #define POT 0

    int errorcw = 0;
    int errorccw = 0;
    int correction = 0;
    double kp = 57;
    double ki = 26;
    double kd = 12;
    int PID = 0;
    int error = 0;
    int last_error = 0;
    int angle = 0;
    int sp = 839;

    void setup()
    {
    Serial.begin(9600);
    pinMode(INA, OUTPUT);
    pinMode(INB, OUTPUT);
    pinMode(PWM1, OUTPUT);
    pinMode(POT, INPUT);

    }

    void loop()
    {
    char buffer[5];
    angle = analogRead(POT);

    if(angle sp)
    {
    errorccw = (angle-sp);
    correction = calcPid(errorccw);

    analogWrite(PWM1, correction);
    digitalWrite(INA, HIGH);
    digitalWrite(INB, LOW);
    }
    // delay(50);
    return;
    }

    int calcPid(int error)
    {
    PID = (kp*(error)) + (ki*(error + last_error)) + (kd*(error – last_error));
    last_error = error;

    return constrain(PID, 0, 255);
    }

    the highlighted error i am getting is
    stray’\’ in program

    and below it says

    PID_Pendulum:55: error: stray ‘\’ in program
    PID_Pendulum.cpp: In function ‘int calcPid(int)’:
    PID_Pendulum:55: error: expected `)’ before ‘u2013′
    PID_Pendulum:55: error: expected `)’ before ‘;’ token

    Thank you so much Andrew i really appreciate the help!

  3. Hmm. I’m not sure what the issue is, but here is the full program that I used:

    Edit:

    I just copied and pasted the code I just put in back into the arduino compiler and it gave me the same error about the \…

    I think that inputting it into the wordpress editor is modifying it somehow.

    Edit:

    I think this should work:

    #define INA 53
    #define INB 51
    #define PWM2 13
    #define POT 0

    int errorcw = 0;
    int errorccw = 0;
    int correction = 0;
    double kp = 57;
    double ki = 0;
    double kd = 0;
    int PID = 0;
    int error = 0;
    int last_error = 0;
    int angle = 0;
    int sp = 839;

    void setup()
    {
    Serial.begin(9600);
    pinMode(INA, OUTPUT);
    pinMode(INB, OUTPUT);
    pinMode(PWM2, OUTPUT);
    pinMode(POT, INPUT);

    }

    void loop()
    {
    char buffer[5];
    angle = analogRead(POT);
    Serial.print("#S|PIDLOG|[");
    Serial.print(itoa((angle), buffer, 10));
    Serial.println("]#");
    if(angle sp)
    {
    errorccw = (angle-sp);
    correction = calcPid(errorccw);

    analogWrite(PWM2, correction);
    digitalWrite(INA, HIGH);
    digitalWrite(INB, LOW);
    }
    // delay(50);
    return;
    }

    int calcPid(int error)
    {
    PID = (kp*(error)) + (ki*(error + last_error)) + (kd*(error - last_error));
    last_error = error;

    return constrain(PID, 0, 255);
    }

    • Mr. Brown permalink

      I just wanted to point out for anybody who is having problems with the code that WordPress is leaving off a less than sign (<) in the code:

      Original Code: if(angle sp)

      What is should be: if(angle < sp)

      Once I added the "<" it was able to compile (verify) the code for me.

  4. Ahmedi Nejad permalink

    What is the PWM2 ? Thank you.

    • PWM2 is the motor speed output to the motor controller. INA and INB are the on/off states that my specific motor controller required, it could not simply use a single input channel for in/off

  5. Steve permalink

    I meant Andrew…I appreciate any help!!

    • Hey Steve,
      I will email you the original code, I think wordpress seems to mess it up.

      I have defined pin0 to be an input from POT or the potentiometer
      Pin 12 and 13 are INA and INB which are outputs from the arduino to the motor controller for on/off
      My motor controller required that I send it both on and off digital voltages, but they are basically both just an on/off switch for my particular motor controller.

      I defined pin 13 as the PWM output to the motor controller for motor speed.

      -Andrew

    • Here’s a yousendit link with the code:
      https://www.yousendit.com/download/WUJZN3RkNmNWRCs5TE5Vag
      if it asks for a password it should be

      arduino

  6. Alexander permalink

    Hello Andrew
    Could you please write which exactly DC motor model did you use (and with which reduction ratio)?
    Thank you,
    Alexander

  7. pabloarevalo permalink

    Hi!! , I have a question, what voltage are you using for the motor?? 24V is ok?? or is it too much? , than you!!

    • Hey Pablo,
      I think 24 v may be too much, the motor rating are listed online
      http://www.pololu.com/product/1092
      -Andrew

      • pabloarevalo permalink

        Thanks for your answer Andrew , I have another question, I am doing the same project and I built a completely different structure for the pendulum movement, but it keeps the same idea, does your code would work??

  8. johnDonahue permalink

    Andrew,

    I’m using the basic ideas of this project to run an experiment on oscillatory motion. I have a few questions:

    1. I’m wondering how you wired the motor controller to both the motor and the arduino. Their website isn’t giving me much information as to what side should be the input, what side should be the output, and why their is Vin on both sides.
    2. What did you use to power the motor controller? If it was a simple battery which one?
    3. Also do you have any recomendations on how to power the motor to move in a sinusoidal motion? I’m planning on using an accelerometer and double integrating to get a position, or using a rotational encoder attached to the drive shaft to find position.

    Thanks for your time!

    • Hey John,

      1. The motor controller I used was hooked up to the pwm outputs on the arduino (should say pwm) then you have the AIN1 and AIN2 and BIN1 and BIN2 which set the direction of the motor. I hooked these up to digital out (either high or low) on the arduino. There are two voltage inputs, one is what powers the board, I hooked it up to the 5v output on the arduino, and vmot which in the input from the battery pack. Also ground to ground of the board and the battery pack.

      2. See above. Motor controller board is powered by arduino output for mine

      3. As far as sinusoidal motion… I’m not sure but there may be sin functions in the arduino language you can use… I’d set the output to be scaled between some factor of 0 and 255 which is the output range of the pwm output.

      -Andrew

  9. David permalink

    Hi Andy could you please tell me where i can buy all the part for the inverted pendulum i am from Australia, i want to build one for my own but i do not known where to buy?

    • Hey David,
      I bought most of my supplies from either sparkfun.com or pololu.com

      I don’t think they’re necessarily the cheapest, but they have most of the things you would need

  10. David permalink

    Hi Andy could you please tell me about the pendulum, how long and the weight of it ? Thanks.

  11. Vishnu permalink

    Can you please tell me the current and voltage rating of the battery pack that used for power up motor?

    • I used 4 “D” cells in series, so it should be 6 volts at around 1 amp, but I don’t think it should be drawing anywhere near 1 amp.

  12. Harry permalink

    Hi Andy,
    Can you tell me how you connect the potentiometer to the Arduino as your picture is not clear.
    Thanks,

    Harry

    • I believe the potentiometer should be connected to the analog in pins on the arduino. These perform ADC or analog to digital conversion, and should give a reading between 0 and 1024 or whatever the resolution of the ADC is on the arduino you have.

  13. Hi Andy we built the inverted pendulum already but we stuck at the connection from the arduino to motor controller via breakboard. could you please tell us how it connect also with the motor controller do you use two power input or just one( 1 from the arduino board and 1 from power pack batteries ). Thank you Andy.

    • Hey Lu,

      Depending on what motor controller you have, you will need to connect the PWM input on the motor controller to the PWM output on the arduino. Then you also need to connect the INA to a digital out on the arduino, this connection tells the motor whether it is going forward or backward. Also depending on the motor controller, it may require its own power source to power the board itself, but also there will be a secondary power source that drives the motors themselves, which is connected to the battery power source. This can have its own ground, but also the ground of the board I believe should be connected to the ground pin of the arduino.

  14. Harry permalink

    Hi Andy,

    I am using the Md03a motor driver from pololu. I have connected the motor to output A and B terminal on this motor controller and connect the battery to the Vin terminal. But it seem the motor not running at all. When I measure the voltage from these output, it shown that there is no power. Could you tell me how to power that motor and what I have done is correct or not.

    It is necessary that I have to connect the arduino and the motor controller through the breakboard?

    Thanks Andy

  15. Harry permalink

    Hi Andy,

    Could you please tell me how can you get the motor going backward and forward with INA and INB as I can only made it go on one direction. Also is it something to do with the potentiometer, when the poteniometer running it will give the direction for the motor to go backward or forward, is it?
    Thanks,

    Harry

  16. Raheel Khan permalink

    Hi Andre,
    We are controlling the two task at a time.
    1) Balancing of Inverted Pendulum.
    2) Cart position controlling.
    We are balancing the pendulum up till now by using the above program but facing the issue to control the position of the cart. Can you help us? We are final year students working on our Final Year Project and submission date is near, we really need your help. We are finding a person who can help us in controlling the position of the cart. if it’s possible for you, please contact us on E-mail.

    Thankyou, waiting for your positive response.

    -Raheel Khan

  17. Ted permalink

    Hey Andy, you mention that the “drawing files of the components to be machined” are attached, but I don’t see any files.

    • Hey ted, I have uploaded a .zip file with the solidworks and arduino code.

      • Saurabh permalink

        Andy , we have compiled above program for our project. Its not showing any error but motor is not running.
        Plz provide some assistance

      • What motor controller are you using?

        Are you able to run the motor both forward and backward using other codes?

  18. Babalola permalink

    Hello,

    I am working on an inverted pendulum and I’m have problems with the coding aspect. First is the microcontroller code for the arduino and also the coding to prevent the pendulum cart from touching the walls of the pendulum frame. I will appreciate if you can help me in these regards. Thank you.

    Babalola

  19. rohit sawa permalink

    hello andy,
    we are facing some problem in programming, its compiling but we are not able to run motor.
    what is the meaning of sp=839??

    • sp=839 is the set point of the potentiometer, since the arduino has a 10 bit analog to digital converter, it discretizes the potentiometer output into 1024 “steps”.

      It turns out that based on the way I had my potentiometer set up, that 90 degrees (vertical) coincided with approximately with step 839 out of 1024 possible.

      The minimum point on the potentiometer would be “0”, the middle would be “512” and the maximum would be “1024” ideally.

      You will have to adjust the set point yourself by figuring out what potentiometer setting corresponds with vertical in your assembly.

  20. hello Andy.thanxx for the code .I am facing some problem in running the motor with the programm .Plzz help me

  21. Diana permalink

    Hello Andy. I’m doing a similar project for a university project. I would like to know if you had to make a process to make discrete the problem of the inverted pendulum. I was looking for a discrete PID and I stop by your code but I don’t know if it is discrete or not. Is it discrete? Which strategy did you use?

    • This should be what is considered a discrete PID controller, for tuning I used the Zeigler-Nichols tuning method.

  22. chin permalink

    Hi Andy,
    Are you using a low torque potentiometer to measure the angle of the inverted pendulum? c
    Does a high torqe potentiometer work as well?
    Chin

    • You could use a high torque potentiometer, as long as your inverted pendulum is rigidly attached to the potentiometer, I don’t see why not, it’ll just slow everything down which could be an advantage. It would be similar to increasing the length of the pendulum or increasing the friction of the pivot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: