How can I read data in Arduino IDE from a text file, if I have a text file which has the angle of arm at the different time intervals? If I want to read a text file from PC, can anyone please show the demonstration code as well? I am attaching a code where I am writing a position to servo1 whose values I want to give via text file.

#include <Servo.h>

Servo myservo1 ;   
int pos1 = 0;     

void setup() {
  myservo1.attach(4);   //inner

void loop() {
   myservo1.write(pos1); // pos1 I will give different angles via text file

If I am not using SD card module, how can I do this with PC only? Someone please help.

  • Can you show us the code you have written so far? Are you using an SD card reader module? Perhaps processing could be used to communicate between the PC and Arduino? Please update your question with more information. – VE7JRO Jan 9 at 18:55
  • I edited accordingly with the code, now someone reply, please. – Shivam Goswami Jan 9 at 19:11
  • Use python to read the text file and send it to the arduino via serial. Use the serial library for python and Serial on the arduino side. – Sim Son Jan 9 at 19:17
  • your code has nothing to do with the problem ... you need to realize that the data transfer between a PC and the Arduino is totally separate from the servo control ... what you do with the received data is irrelevant ... at this point you need to solve the data transfer problem – jsotola Jan 9 at 19:31

I assume you want to store multiple values in the text file, and then have the Arduino IDE (not the Arduino itself) read the file and insert the data into your sketch. If this is really what your question is about, a quick and dirty solution would be as follows:

  1. Format the text file as a list of comma-separated numbers, either in one or several lines. For example:
1, 12, 23, 34, 45, 56, 67,
78, 89, 100
  1. Include this file in your sketch as the body of an array initializer:
#include <Servo.h>

const int positions[] = {
#include "full_path_of_the_file_holding_the_positions"
#define positions_length (sizeof positions / sizeof positions[0])

Servo myservo1;   

void setup() {

void loop() {
    for (int i = 0; i < positions_length; i++) {
  • This helped me somewhat, thanks. Now I have a lot of variables and data that I will be sending to arduino IDE, Is there any size limit that Arduino accepts. I have a very big set of data for SERVO control. – Shivam Goswami Jan 12 at 18:19
  • @ShivamGoswami: Each board has a fixed amount of available flash and RAM memory. If you have lots of constant data, PROGMEM should help: do a Web search for that term. – Edgar Bonet Jan 13 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.