# Writing the code

Once when you have decided the appropriate hardware configuration of yous board and once you have all the hardware ready to be connected:

• Microcontroller
• BLDC motor
• Position sensor
• Power supply

we can start the most exciting part, coding!

Arduino SimpleFOCShield is fully supported by Arduino SimpleFOClibrary, therefore please make sure you have the newest version of the SimpleFOClibrary installed. If you still did not get your owm version of the library please follow the installation instructions.

Suggested approach when starting coding for the Arduino SimpleFOCShield is:

You can also follow our Getting started guide!

## Step 1. Testing the sensor

First make sure your sensor works properly. Run one of the library examples specific to your sensors. You can find the library examples in

utils >
sensor_test >
encoder >
- encoder_example
- encoder_software_interrupts_example
magnetic_sensors >
- magnetic_sensor_i2c_example
- magnetic_sensor_spi_example
- magnetic_sensor_analog_example
hall_sensors >
- hall_sensor_example
- hall_sensor_software_interrupts_example


Once you have your sensor reading the good values you can continue the testing the combination of the motor and the sensor.

Update the example pinout

When testing the sensor make sure to update the pinout that you have chosen in the hardware configuration.

## Step 2. Testing the motor

To test the BLDC motor before running FOC algorithm we suggest to run the open loop examples!

motion_control >
open_loop_motor_control >
- open_loop_position_example
- open_loop_velocity_example


Update the example pinout

When testing the motor make sure to update the pinout that you have chosen in the hardware configuration.

If you are not sure what is the number of pole pairs your motor has, please check the example code:

utils >
find_pole_pair_number >
- encoder
- magnetic_sensor


This code will estimate the pole pairs number that your motor has. Please run this code several times to get a good estimation. The code will, generally show a good reading 7/10 times.

## Step 3. Voltage motion control

Once you have your sensor working, and you have the right number of pole pairs of your motor you can start using FOC algorithm. The best practice is to start with an example of the Voltage control:

motion_control >
torque_voltage_control >
- encoder
- magnetic_sensor
- hall_sensors


## Step 4. More complex control strategies

Now when you have your torque control using voltage ready you can continue to the position and velocity control algorithms. They will take a bit more time to tune but they will give you a chance to have great results. You can find the library examples for motion control loops in by navigating the examples:

motion_control >
position_motion_control >
- encoder
- magnetic_sensor
- hall_sensors
torque_voltage_control >
- encoder
- magnetic_sensor
- hall_sensors
velocity_motion_control >
- encoder
- magnetic_sensor
- hall_sensors


For more information about the possible SimpleFOCShield projects visit example projects