How can I simulate an accelerometer?










1















I am new to Arduino. I want to simulate a circuit which uses an accelerometer ADXL335 and a DC motor. If any deviation observed in the accelerometer readings, I want the motor to rotate. Is there any way I can simulate its working in software without implementing it?










share|improve this question




























    1















    I am new to Arduino. I want to simulate a circuit which uses an accelerometer ADXL335 and a DC motor. If any deviation observed in the accelerometer readings, I want the motor to rotate. Is there any way I can simulate its working in software without implementing it?










    share|improve this question


























      1












      1








      1








      I am new to Arduino. I want to simulate a circuit which uses an accelerometer ADXL335 and a DC motor. If any deviation observed in the accelerometer readings, I want the motor to rotate. Is there any way I can simulate its working in software without implementing it?










      share|improve this question
















      I am new to Arduino. I want to simulate a circuit which uses an accelerometer ADXL335 and a DC motor. If any deviation observed in the accelerometer readings, I want the motor to rotate. Is there any way I can simulate its working in software without implementing it?







      accelerometer






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 10:12









      Michel Keijzers

      6,67441838




      6,67441838










      asked Nov 14 '18 at 7:01









      Hrithik BaishakhiyaHrithik Baishakhiya

      61




      61




















          1 Answer
          1






          active

          oldest

          votes


















          3














          Your sensor supplies an analog signal, ranging from 0V for -3g to 3.3V for +3g.
          This means you have a signal averaging at "338" when read using Arduino's analogRead() function, with spikes in either direction when acceleration changes.



          You would want some code which gives you a variable hovering around "338", with random acceleration spikes injected in either direction. This could look something like this:



          int16_t num_zerog = (3.3 / 5) * 512;
          int16_t num_xaxis = num_zerog;
          uint8_t num_random = 0;

          void setup()
          Serial.begin(115200);


          void loop() num_xaxis > 675)
          num_xaxis = num_zerog;

          Serial.println(num_xaxis);
          delay(10);



          Explanation: First, the value for 0g is determined from the voltage difference.
          A random value is generated, ranging from 0 to 255.
          If the random value is 0, a negative value is added to your "signal". If it's 255, a positive spike is added.
          If any other value is obtained, it goes back to it's 0g level with 1/4th of the difference between the current value and the 0g value per loop.



          The serial commands only serve to let you watch the result in Arduino IDE's serial plotter.
          If you need the spike to occur less frequently, adjust the delay(), or let the counter run to higher numbers.
          If the CPU-blocking delay() command is an issue in your project, you could run the randomization code using a timer and an interrupt.



          Please note that real acceleration sensors exhibit a lot more noise than this. This could be implemented by adding smaller spikes at a broader range of random values (like -1 if num_random is below 100 and +1 if it is above 155).
          Graph






          share|improve this answer




















          • 1





            An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

            – Edgar Bonet
            Nov 14 '18 at 8:23











          • You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

            – Tobias Weiß
            Nov 14 '18 at 8:48










          Your Answer






          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("schematics", function ()
          StackExchange.schematics.init();
          );
          , "cicuitlab");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "540"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57799%2fhow-can-i-simulate-an-accelerometer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Your sensor supplies an analog signal, ranging from 0V for -3g to 3.3V for +3g.
          This means you have a signal averaging at "338" when read using Arduino's analogRead() function, with spikes in either direction when acceleration changes.



          You would want some code which gives you a variable hovering around "338", with random acceleration spikes injected in either direction. This could look something like this:



          int16_t num_zerog = (3.3 / 5) * 512;
          int16_t num_xaxis = num_zerog;
          uint8_t num_random = 0;

          void setup()
          Serial.begin(115200);


          void loop() num_xaxis > 675)
          num_xaxis = num_zerog;

          Serial.println(num_xaxis);
          delay(10);



          Explanation: First, the value for 0g is determined from the voltage difference.
          A random value is generated, ranging from 0 to 255.
          If the random value is 0, a negative value is added to your "signal". If it's 255, a positive spike is added.
          If any other value is obtained, it goes back to it's 0g level with 1/4th of the difference between the current value and the 0g value per loop.



          The serial commands only serve to let you watch the result in Arduino IDE's serial plotter.
          If you need the spike to occur less frequently, adjust the delay(), or let the counter run to higher numbers.
          If the CPU-blocking delay() command is an issue in your project, you could run the randomization code using a timer and an interrupt.



          Please note that real acceleration sensors exhibit a lot more noise than this. This could be implemented by adding smaller spikes at a broader range of random values (like -1 if num_random is below 100 and +1 if it is above 155).
          Graph






          share|improve this answer




















          • 1





            An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

            – Edgar Bonet
            Nov 14 '18 at 8:23











          • You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

            – Tobias Weiß
            Nov 14 '18 at 8:48















          3














          Your sensor supplies an analog signal, ranging from 0V for -3g to 3.3V for +3g.
          This means you have a signal averaging at "338" when read using Arduino's analogRead() function, with spikes in either direction when acceleration changes.



          You would want some code which gives you a variable hovering around "338", with random acceleration spikes injected in either direction. This could look something like this:



          int16_t num_zerog = (3.3 / 5) * 512;
          int16_t num_xaxis = num_zerog;
          uint8_t num_random = 0;

          void setup()
          Serial.begin(115200);


          void loop() num_xaxis > 675)
          num_xaxis = num_zerog;

          Serial.println(num_xaxis);
          delay(10);



          Explanation: First, the value for 0g is determined from the voltage difference.
          A random value is generated, ranging from 0 to 255.
          If the random value is 0, a negative value is added to your "signal". If it's 255, a positive spike is added.
          If any other value is obtained, it goes back to it's 0g level with 1/4th of the difference between the current value and the 0g value per loop.



          The serial commands only serve to let you watch the result in Arduino IDE's serial plotter.
          If you need the spike to occur less frequently, adjust the delay(), or let the counter run to higher numbers.
          If the CPU-blocking delay() command is an issue in your project, you could run the randomization code using a timer and an interrupt.



          Please note that real acceleration sensors exhibit a lot more noise than this. This could be implemented by adding smaller spikes at a broader range of random values (like -1 if num_random is below 100 and +1 if it is above 155).
          Graph






          share|improve this answer




















          • 1





            An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

            – Edgar Bonet
            Nov 14 '18 at 8:23











          • You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

            – Tobias Weiß
            Nov 14 '18 at 8:48













          3












          3








          3







          Your sensor supplies an analog signal, ranging from 0V for -3g to 3.3V for +3g.
          This means you have a signal averaging at "338" when read using Arduino's analogRead() function, with spikes in either direction when acceleration changes.



          You would want some code which gives you a variable hovering around "338", with random acceleration spikes injected in either direction. This could look something like this:



          int16_t num_zerog = (3.3 / 5) * 512;
          int16_t num_xaxis = num_zerog;
          uint8_t num_random = 0;

          void setup()
          Serial.begin(115200);


          void loop() num_xaxis > 675)
          num_xaxis = num_zerog;

          Serial.println(num_xaxis);
          delay(10);



          Explanation: First, the value for 0g is determined from the voltage difference.
          A random value is generated, ranging from 0 to 255.
          If the random value is 0, a negative value is added to your "signal". If it's 255, a positive spike is added.
          If any other value is obtained, it goes back to it's 0g level with 1/4th of the difference between the current value and the 0g value per loop.



          The serial commands only serve to let you watch the result in Arduino IDE's serial plotter.
          If you need the spike to occur less frequently, adjust the delay(), or let the counter run to higher numbers.
          If the CPU-blocking delay() command is an issue in your project, you could run the randomization code using a timer and an interrupt.



          Please note that real acceleration sensors exhibit a lot more noise than this. This could be implemented by adding smaller spikes at a broader range of random values (like -1 if num_random is below 100 and +1 if it is above 155).
          Graph






          share|improve this answer















          Your sensor supplies an analog signal, ranging from 0V for -3g to 3.3V for +3g.
          This means you have a signal averaging at "338" when read using Arduino's analogRead() function, with spikes in either direction when acceleration changes.



          You would want some code which gives you a variable hovering around "338", with random acceleration spikes injected in either direction. This could look something like this:



          int16_t num_zerog = (3.3 / 5) * 512;
          int16_t num_xaxis = num_zerog;
          uint8_t num_random = 0;

          void setup()
          Serial.begin(115200);


          void loop() num_xaxis > 675)
          num_xaxis = num_zerog;

          Serial.println(num_xaxis);
          delay(10);



          Explanation: First, the value for 0g is determined from the voltage difference.
          A random value is generated, ranging from 0 to 255.
          If the random value is 0, a negative value is added to your "signal". If it's 255, a positive spike is added.
          If any other value is obtained, it goes back to it's 0g level with 1/4th of the difference between the current value and the 0g value per loop.



          The serial commands only serve to let you watch the result in Arduino IDE's serial plotter.
          If you need the spike to occur less frequently, adjust the delay(), or let the counter run to higher numbers.
          If the CPU-blocking delay() command is an issue in your project, you could run the randomization code using a timer and an interrupt.



          Please note that real acceleration sensors exhibit a lot more noise than this. This could be implemented by adding smaller spikes at a broader range of random values (like -1 if num_random is below 100 and +1 if it is above 155).
          Graph







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 8:46

























          answered Nov 14 '18 at 7:56









          Tobias WeißTobias Weiß

          3277




          3277







          • 1





            An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

            – Edgar Bonet
            Nov 14 '18 at 8:23











          • You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

            – Tobias Weiß
            Nov 14 '18 at 8:48












          • 1





            An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

            – Edgar Bonet
            Nov 14 '18 at 8:23











          • You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

            – Tobias Weiß
            Nov 14 '18 at 8:48







          1




          1





          An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

          – Edgar Bonet
          Nov 14 '18 at 8:23





          An input voltage of 1.65 V (half of 3.3 V) should give you a reading of (1.65 V ÷ 5 V) × 1024 ≈ 338.

          – Edgar Bonet
          Nov 14 '18 at 8:23













          You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

          – Tobias Weiß
          Nov 14 '18 at 8:48





          You're right of course, I forgot that analogRead() defaults to 10 bit. Thank you, post corrected.

          – Tobias Weiß
          Nov 14 '18 at 8:48

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Arduino Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2farduino.stackexchange.com%2fquestions%2f57799%2fhow-can-i-simulate-an-accelerometer%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto