Your First Project
Create your first AI-assisted PLC program step by step
Your First Project
In this tutorial, you'll create a simple motor control function block using PLC Studio AI. By the end, you'll understand the basic workflow of AI-assisted PLC development.
Prerequisites
Before starting, make sure you have:
- ✅ PLC Studio AI account created
- ✅ CODESYS bridge installed and working
- ✅ A CODESYS project open
If you haven't set these up yet, see the installation guide.
What We'll Build
We'll create a function block called FB_SimpleMotor that:
- Starts a motor when a start button is pressed
- Stops the motor when a stop button is pressed
- Includes a run timer to track operating time
- Has an emergency stop that immediately stops the motor
Step 1: Connect to Your Project
- Open CODESYS with your project
- Start the PLC Studio AI bridge
- Open PLC Studio AI in your browser
- Verify your project appears in the Project Explorer
You should see your project structure in the left sidebar.
Step 2: Create a New Function Block
Let's ask the AI to create our function block.
In the AI chat, type:
Create a new function block called FB_SimpleMotor with these features:
- Inputs: bStart (BOOL), bStop (BOOL), bEmergencyStop (BOOL)
- Outputs: bRunning (BOOL), tRunTime (TIME)
- When bStart goes TRUE and motor is not running, start the motor
- When bStop goes TRUE, stop the motor
- bEmergencyStop immediately stops the motor regardless of other inputs
- tRunTime should track how long the motor has been runningStep 3: Review the Generated Code
The AI will generate code similar to this:
FUNCTION_BLOCK FB_SimpleMotor
VAR_INPUT
bStart : BOOL;
bStop : BOOL;
bEmergencyStop : BOOL;
END_VAR
VAR_OUTPUT
bRunning : BOOL;
tRunTime : TIME;
END_VAR
VAR
tonRunTimer : TON;
bStartEdge : BOOL;
bPrevStart : BOOL;
END_VAR
// Detect rising edge of start
bStartEdge := bStart AND NOT bPrevStart;
bPrevStart := bStart;
// Motor control logic
IF bEmergencyStop THEN
bRunning := FALSE;
ELSIF bStop THEN
bRunning := FALSE;
ELSIF bStartEdge AND NOT bRunning THEN
bRunning := TRUE;
END_IF
// Run time tracking
tonRunTimer(IN := bRunning, PT := T#24H);
IF bRunning THEN
tRunTime := tonRunTimer.ET;
ELSE
tRunTime := T#0S;
END_IFTake a moment to review:
- Are the variable names appropriate?
- Does the logic match what you wanted?
- Is the timer implementation correct?
Step 4: Accept the Changes
If the code looks good:
- Click Accept in the diff view
- The code will be sent to CODESYS
- Watch for compilation results
If you see any errors, the AI can help fix them.
Step 5: Test the Function Block
Now let's use our function block in a program.
Ask the AI:
Create a test program PLC_PRG that:
- Instantiates FB_SimpleMotor as fbMotor
- Connects it to some test variables for manual testingThe AI will generate:
PROGRAM PLC_PRG
VAR
fbMotor : FB_SimpleMotor;
// Test inputs
bTestStart : BOOL;
bTestStop : BOOL;
bTestEStop : BOOL;
// Monitor outputs
bMotorRunning : BOOL;
tMotorRunTime : TIME;
END_VAR
// Call the motor function block
fbMotor(
bStart := bTestStart,
bStop := bTestStop,
bEmergencyStop := bTestEStop
);
// Copy outputs for monitoring
bMotorRunning := fbMotor.bRunning;
tMotorRunTime := fbMotor.tRunTime;Step 6: Enhance with AI
Let's add a feature. Ask the AI:
Add a speed input (rSpeed: REAL, 0-100%) and speed output to FB_SimpleMotor.
The motor should only run if speed is greater than 0.Review and accept the changes.
Step 7: Add Error Handling
One more enhancement:
Add error detection to FB_SimpleMotor:
- bError output that goes TRUE if speed exceeds 100%
- nErrorCode output: 0 = no error, 1 = overspeed
- Motor should not run while bError is TRUECongratulations!
You've created your first AI-assisted function block! You learned how to:
- ✅ Create new code with natural language prompts
- ✅ Review and accept AI-generated changes
- ✅ Iteratively enhance code with additional requests
- ✅ Handle compilation feedback