echo '' ;

Archives

Arduino Project – Servo Control Using Accelerometer ADXL345

This article is a continuation of the series on Arduino Project – Servo Control Using Accelerometer ADXL345 and carries the discussion on how to control the motor using the sensor.

Prerequisites

To continue with this tutorial, you must have read the below articles

Components Needed:

  • Arduino IDE (Web or Installed on the machine)
  • PC
  • Servo Motor
  • ADXL345 Accelerometer Sensor
  • Arduino Board (e.g., Arduino Uno)
  • Jumper wires
  • Breadboard

Connection Diagram

Arduino Project Servo Control Using Accelerometer ADXL345
Arduino Project Servo Control Using Accelerometer ADXL345

Wiring

  • Connect the VCC pin of the ADXL345 to the 3.3V pin on the Arduino.
  • Connect the GND pin of the ADXL345 to the GND pin on the Arduino.
  • Connect the SDA pin of the ADXL345 to the A4 pin on the Arduino.
  • Connect the SCL pin of the ADXL345 to the A5 pin on the Arduino.
  • Connect the signal wire of the servo motor to a digital pin on the Arduino (e.g., Pin 5).
  • Connect the power (VCC) and ground (GND) of the servo motor to the corresponding pins on the Arduino.

Arduino Library used List

Arduino libraries are collections of pre-written code that simplify complex tasks and allow users to easily integrate functionality into their Arduino projects. Libraries provide functions and routines that can be used to perform various tasks without the need for users to write the code from scratch. Arduino libraries are written in C or C++ and can be shared and reused by the Arduino community

Code

/* ArunEworld - @2024
Accelerometer connection pins (I2C) to Arduino are shown below:

Arduino     Accelerometer ADXL345
  A5            SCL
  A4            SDA
  3.3V          CS
  3.3V          VCC
  GND           GND
  
Arduino     Servo No. 1
  5V          5V  (Red Wire)
  GND         GND (Black Wire)
  D5          (last wire for control - might be white color)
  
Arduino     Servo No. 2
  5V          5V  (Red Wire)
  GND         GND (Black Wire)
  D6          (last wire for control - might be white color)
*/

#include <Wire.h>
#include <ADXL345.h>

#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2;

ADXL345 adxl; //variable adxl is an instance of the ADXL345 library

int x, y, z;
int rawX, rawY, rawZ;
int mappedRawX, mappedRawY;

void setup() {
  Serial.begin(9600);
  adxl.powerOn();
  servo1.attach(5);
  servo2.attach(6);
}

void loop() {
  adxl.readAccel(&x, &y, &z); //read the accelerometer values and store them in variables  x,y,z

  rawX = x - 7;
  rawY = y - 6;
  rawZ = z + 10;
  
  if (rawX < -255) rawX = -255; else if (rawX > 255) rawX = 255;
  if (rawY < -255) rawY = -255; else if (rawY > 255) rawY = 255;
   
  mappedRawX = map(rawX, -255, 255, 0, 180);
  mappedRawY = map(rawY, -255, 255, 0, 180);
  
  servo1.write(mappedRawX);
  delay(15);
  servo2.write(180 - mappedRawY);
  delay(15);
  
  Serial.print(" mappedRawX = "); Serial.print(mappedRawX); // raw data with offset
  Serial.print(" mappedRawY = "); Serial.println(mappedRawY); // raw data with offset
}

Testing

  1. Power up your Arduino board.
  2. Tilt the ADXL345 accelerometer in different directions to observe changes in the accelerometer data on the Serial Monitor.
  3. The servo motor should move in response to the accelerometer data, adjusting its position based on the tilt of the accelerometer.

Troubleshooting

  • If the servo doesn’t move as expected, check the wiring and make sure all connections are secure.
  • Adjust the servo angle mapping and constraints in the code if needed.
  • Ensure that the accelerometer is correctly connected and functioning.

That’s it! You should now have a working setup where the servo motor is controlled based on the tilt data from the ADXL345 accelerometer. Feel free to experiment with the code and make modifications to suit your specific requirements.

NEXT

Arduino-Get Start
Arduino Interface
Arduino Interface-LED
Arduino Interface-Button
Arduino Interface -Buzzer
Arduino Interface-ADC
Arduino Interface-UART(Serial)
Arduino Interface-PWM
Arduino Interface-RGB LED
Arduino Interface-LCD
Arduino Tutorials
Random Number Generator
Voltage Measurement
Arduino Projects
Therimine
Water Flow Meter
Servo Control Using Accelerometer ADXL345
Others
Arduino-Course
Arduino-Sitemap
Arduino-FAQ

Arduino – Getting Started

Arduino is an electronic prototyping platform. It is an Open-source and users to create interactive electronic objects. Arduino is an open-source hardware and software company, project and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices. It supports various CPUs like Atmel AVR (8-bit), ARM Cortex-M0+ (32-bit), ARM Cortex-M3 (32-bit), and Intel Quark (x86) (32-bit).

Read more… →

Arduino Tutorial – Random Number Generator

This tutorial is about “Arduino Tutorial – Random Number Generator”. A Random Number Generator (RNG) is a computational or physical process designed to generate a sequence of numbers that lack any pattern or predictability. These numbers are typically used in various fields such as cryptography, simulations, gaming, and statistical sampling.

Types of RNGs

RNGs play a vital role in ensuring fairness in games, maintaining security in cryptographic systems, and simulating complex phenomena in scientific research and engineering simulations.

Pseudorandom Number Generators (PRNGs): These algorithms generate numbers that appear random but actually derive from an initial value called a seed. PRNGs are deterministic, meaning if you know the seed, you can reproduce the sequence of numbers. Software applications widely use them due to their efficiency and ease of implementation.

True Random Number Generators (TRNGs): TRNGs generate numbers based on unpredictable physical processes, such as radioactive decay, thermal noise, or atmospheric noise. TRNGs provide higher security and are common in cryptographic applications where unpredictability is crucial because they rely on inherently random phenomena.

Use of Random Number Generator

FieldUses
CryptographyGenerating cryptographic keys, initialization vectors, and nonces
Ensuring the security of encryption algorithms and digital signatures
GamingSimulating chance-based events in video games, casinos, and gambling platforms
Ensuring fairness in gaming outcomes
Simulation and ModelingGenerating random inputs for simulations across multiple disciplines
Adding stochastic elements to models for realism
Statistical SamplingGenerating random samples for hypothesis testing and estimation
Reducing bias in statistical analyses
Monte Carlo MethodsEstimating complex mathematical problems using random sampling
Simulating probabilistic systems
Machine LearningRandom initialization of model parameters
and Artificial IntelligenceShuffling datasets for training
Introducing randomness in optimization algorithms
Numerical AnalysisImproving performance and convergence properties of algorithms

Prerequisites

Before start to learn, you should know below topics. If you know already, please go further

Components Required for Arduino Tutorial Random Number Generator

  • Arduino  Uno Board (You Can use any other arduino boards*) – 1 Nos

Code : randomSeed()

Arduino Tutorial Random Number Generator

long randNumber;

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  randNumber = random(300);
  Serial.println(randNumber);

  delay(50);
}

Code Explanation

Component/FunctionPurpose
long randNumber;Variable declaration to store the generated random number
void setup()Function that runs once to initialize the Arduino board and setup necessary configurations
Serial.begin(9600);Initializes serial communication with the computer at a baud rate of 9600 bits per second
randomSeed(analogRead(0));Seeds the random number generator with a value obtained from reading analog pin 0
void loop()Function that runs continuously after the setup() function, where the main code resides
randNumber = random(300);Generates a random number between 0 and 299 and assigns it to randNumber variable
Serial.println(randNumber);Prints the generated random number to the serial monitor followed by a newline character
delay(50);Inserts a delay of 50 milliseconds between each iteration of the loop

Code: random()

long randNumber;

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  randNumber = random(300);
  Serial.println(randNumber);

  delay(50);
}

Code Explanation

Component/FunctionPurposelong randNumber;Declares a variable to store the generated random numbervoid setup()Initializes the Arduino board and sets up necessary configurationsSerial.begin(9600);Initializes serial communication with the computer at a baud rate of 9600 bits per secondrandomSeed(analogRead(0));Seeds the random number generator with a value obtained from reading analog pin 0void loop()Executes the main code continuously after the setup, where the random number generation occursrandNumber = random(300);Generates a random number between 0 and 299 and assigns it to randNumber variableSerial.println(randNumber);Prints the generated random number to the serial monitor followed by a newline characterdelay(50);Adds a delay of 50 milliseconds between each iteration of the loop


NEXT

Arduino-Get Start
Arduino Interface
Arduino Interface-LED
Arduino Interface-Button
Arduino Interface -Buzzer
Arduino Interface-ADC
Arduino Interface-UART(Serial)
Arduino Interface-PWM
Arduino Interface-RGB LED
Arduino Interface-LCD
Arduino Tutorials
Random Number Generator
Voltage Measurement
Arduino Projects
Therimine
Water Flow Meter
Servo Control Using Accelerometer ADXL345
Others
Arduino-Course
Arduino-Sitemap
Arduino-FAQ

Arduino – Course

Requirements

  • A Windows, Mac or Linux computer
  • An Arduino Uno
  • Electronics parts like resistors, LEDs, sensors, as listed in Section 1 of the course
  • Essential tools: a mini breadboard, jumper wires, a multimeter, a soldering iron and solder, wire cutter
  • Be excited about electronics!

Read more… →

Arduino Tutorial – Voltage Measurement

Circuit

 

Schematics

 

Code

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}

 

Arduino Interface – Buzzer

In the realm of Arduino Interface Buzzer, can bring your projects to life with sound. We’ll explore how to connect and control a buzzer, from simple beeps to complex melodies. Throughout this guide, we’ll cover wiring, coding, and examples, whether you’re new to Arduino or a seasoned maker. Let’s dive in and unlock the potential of Arduino buzzers together!

So, let’s embark on this sonic journey and unlock the potential of Arduino interfacing with buzzers!

uses of buzzers

Use CaseDescription
Alarm SystemsBuzzers are commonly used in alarm systems to provide audible alerts for security breaches or emergencies.
Timers and RemindersBuzzers in timers and reminders signal task completion or remind users of events.
NotificationsBuzzers in electronics notify users of messages, alerts, or events.
Industrial MachineryBuzzers in industrial machinery signal malfunctions, task completion, or safety alerts.
Games and ToysIn gaming, arcade machines, and toys, buzzers create sound effects, enhancing the experience.
DIY ProjectsMakers use buzzers in DIY projects like interactive installations, instruments, or home automation.

Application of Buzzer

ApplicationDescription
Alarm SystemsBuzzers in security systems offer audible alerts for intrusions or emergencies.
Timer and Reminder SystemsBuzzers signal task completion or remind users of appointments in timers and reminders.
Industrial MachineryIn industry, buzzers in machinery signal errors, task completion, or safety alerts.
Home AppliancesBuzzers in appliances like microwaves, washers, and dishwashers signal cycle end or errors.
AutomotiveIn cars, buzzers signal seatbelt warnings, parking alerts, or low fuel levels.
Games and EntertainmentBuzzers enhance gaming experiences in consoles, arcades, and interactive toys with sound effects.

Circuit Diagram

Read more… →

Arduino Interface – UART(Serial)

This tutorial is “Arduino Interface UART(Serial)”. The Universal Asynchronous Receiver-Transmitter (UART) is a fundamental component in microcontroller communication, enabling data exchange between devices. In the realm of Arduino, mastering UART opens doors to interfacing with a plethora of sensors, actuators, and other devices.

Read more: Arduino Interface – UART(Serial)

In this guide, we’ll delve into the basics of UART communication with Arduino. Whether you’re a hobbyist embarking on your first Arduino project or an experienced developer seeking a refresher, this tutorial aims to demystify UART and equip you with the knowledge to integrate it seamlessly into your projects.

Let’s embark on this journey to unravel the intricacies of UART communication with Arduino, from understanding the principles behind UART to implementing it in your own circuits and code.

Use cases

The Arduino UART (Serial) interface offers a wide range of uses across various projects and applications.

ApplicationDescription
Sensor IntegrationInterface with various sensors like temperature sensors, IMUs, GPS modules for data collection.
Wireless CommunicationEstablish wireless links using Bluetooth, Wi-Fi, or Zigbee modules for remote control & IoT.
Display OutputCommunicate with LCDs, OLEDs to present information in projects like digital clocks, weather stations.
Data LoggingLog data to external storage devices for long-term recording in environmental monitoring, tracking systems.
Human-Machine InterfaceCommunicate with external devices like keypads, RFID readers for user interaction in systems.
Control InterfacesControl motors, relays, servo motors for robotics, automated systems, or interactive installations.
Debugging and Serial CommunicationUse for debugging, real-time monitoring, and data transfer between Arduino and PC.
Interfacing with Other DevicesCommunicate with other microcontrollers like Raspberry Pi, ESP8266, enabling collaborative projects.

Print Hello World in Serial terminal

Code

void setup() 
{
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("ArunEworld : Hello World");
}
 
void loop() 
{
 
}
Read more… →

Arduino Interface – RGB LED

The Arduino microcontroller platform’s versatility in interfacing with various components has earned it renown, making it a go-to for electronics and DIY projects. This tutorial is about “Arduino Interface RGB LED”. One exciting application is controlling RGB (Red, Green, Blue) LEDs to create a spectrum of colors, perfect for beginners and enthusiasts alike.

Read more… →

Arduino Interface – LED

This article is a continuation of the series on “Arduino Interface – LED” and carries the discussion on Turn ON/OFF, and blinking of LED in the Arduino Environment.

  • LED (light-emitting diode): LED is a simple diode that emits light in a forward bias
  • Arduino: is an open-source, board that has a Microchip ATmega328P microcontroller on it.

Pre-Request

  • PC
  • Arduino IDE setup or web-based IDE

Components Required

  • LED – 1 Nos,
  • Resistor, 220 Ohm – 1Nos
  • Breadboard (Optional: If required)
  • Arduino UNO
  • Jumper wires

Turn On/Off LED

The program simply turns ON and OFF LED with some delay between them.

  • For Turn OFF LED – Set digital pin Low digitalWrite(LED_BUILTIN, LOW);
  • For Turn ON LED – Set digital pin high digitalWrite(LED_BUILTIN, HIGH);

 

Read more… →