Maker Pro
Maker Pro

Sketch help - 3 axis accelerometer

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I am trying to output data to a 20x4 lcd using a 3 axis accelerometer and pro micro. All I can get is the first digit of each axis to display. Can someone take a look at the sketch and tell me what I may have wrong?

Code:
/*
ADXL3xx

Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer.  The pins used are designed to be easily
compatible with the breakout boards from Sparkfun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80

http://www.arduino.cc/en/Tutorial/ADXL3xx

The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc

created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

*/

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);


// these constants describe the pins. They won't change:
const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

void setup() {
  // initialize the serial communications:
  Serial.begin(9600);

  lcd.begin();
  lcd.backlight();

  // Provide ground and power by using the analog inputs as normal
  // digital pins.  This makes it possible to directly connect the
  // breakout board to the Arduino.  If you use the normal 5V and
  // GND pins on the Arduino, you can remove these lines.
  //pinMode(groundpin, OUTPUT);
  //pinMode(powerpin, OUTPUT);
  //digitalWrite(groundpin, LOW);
  //digitalWrite(powerpin, HIGH);
}

void loop() {

  //Print the value of the x pin to the first line of the lcd
  lcd.setCursor(0,0);   //set the cursor to the first line
  lcd.print(" X= ");            //display 'X=' followed by
  lcd.print(analogRead(xpin));  //the value of the xpin data

  // Print the value of the y pin to the second line of the lcd
  lcd.setCursor(0,1);   //set the cursor to the second line
  lcd.print(" Y= ");            //display 'Y=' followed by
  lcd.print(analogRead(ypin));  //the value of the xpin data

  // Print the value of the z pin to the third line of the lcd
  lcd.setCursor(0,2);   //set the cursor to the third line
  lcd.print(" z= ");            //display 'Z=' followed by
  lcd.print(analogRead(zpin));  //the value of the xpin data
 
  // print the sensor values to the serial monitor:
  Serial.print(analogRead(xpin));
  // print a tab between values:
  Serial.print("\t");
  Serial.print(analogRead(ypin));
  // print a tab between values:
  Serial.print("\t");
  Serial.print(analogRead(zpin));
  Serial.println();
  // delay before next reading:
  delay(100);
}

Accelerometer LCD2004 I2C.jpg
 

Attachments

  • Accelerometer_LCD2004_I2C.ino
    2.6 KB · Views: 57

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
You have no scaling. You need to scale your input voltage. Like muliplying by 5/1024. Your supply voltage and resolution of the AtoD 10 bit 12 bit ...?....
Adam
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
I have no idea how to do that. I also have no idea if it's 10 or 12 bit. I will research it and try again.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Ok. The Pro Micro has four 10 bit ADc pins. So I should figure the formula as this: 5/1024.

So far I have tried a few things.

Code:
int xpinout = analogRead(5/1024);
lcd.print(analogRead(xpinout));

Code:
lcd.print(analogRead(xpin*1024));

Neither of these things will print the 3 digit output that the serial monitor shows.

Suggestions???
 
Last edited:

Bluejets

Oct 5, 2014
6,993
Joined
Oct 5, 2014
Messages
6,993
Forget the analog device and try to get it to print "hello world " first.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Forget the analog device and try to get it to print "hello world " first.

I can do that. I have done other projects with the LCD. Why it only shows 1 digit has me boggled. Correctly wording the formula in the sketch is eating my lunch.
 

Bluejets

Oct 5, 2014
6,993
Joined
Oct 5, 2014
Messages
6,993
When reading an analog value, I thought it was a requirement to first read the analog value into a variable and then print that variable. ?????

e.g. .......... sensorX=analogRead(xpin);
lcd.print(sensorX);
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
Here is some code I put together for an LCD I was messing around with. It prints the voltage on the analog pin to the LCD. My LCD was an 8*2 so you might have to change the settings at the top of the code.
Hope this helps
Adam

Code:
/* Tutorial 5: Interfacing an HD44780 based character LCD Description: Explore the various LCD display commands available through the built-in LiquidCrystal library. Board: chipKIT UNO32 */     // Include the LiquidCrystal library
#include <LiquidCrystal.h>   // Initialize the LCD interface pins in the following sequence // RS, E, D4, D5, D6, D7
const int channel = 0;
const float conv = 3.30/1024;
int DAC;
float mV;
 
 
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);  
void setup() {
// Define the number of columns and rows of the LCD
lcd.begin(8, 2);
}
void loop() {
              // lcd.print() demo
lcd.print("  DVM");
analogReference(INTERNAL);
lcd.setCursor(0, 1); // Set cursor to col 0 row 1
DAC=analogRead(channel);
mV=DAC*conv;
 
lcd.print(mV);
lcd.print("V");
delay(2000);
lcd.clear();
}
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Here's what I have:

Code:
  int xdat1 = analogRead(xpin);
  lcd.print (xdat1);

I still only get 1 digit of data on the LCD.

Any ideas? I have tried to use this:

Code:
int xdat = analogRead((xpin(5/1024));
lcd.print(xdat);

But I get the error xpin cannot be used as a function. I then try:

Code:
  int xdat1 = analogRead(xpin);
  lcd.print (xdat1(5/1024));

But get the error that xdat1 cannot be used as a function.

I will continue to try things that come to mind. Thanks.
 

bigone5500

Apr 9, 2014
712
Joined
Apr 9, 2014
Messages
712
Did you try my code?

Actually I just did. I added the following:

Code:
const float conv = 5/1024;
int xdat;
float xdat1;


xdat = analogRead(xpin);
xdat1 = xdat*conv;
lcd.print(xdat1);

I now get 0.00 on the first line of the LCD. That is great. But now how to get the correct data to display.
 

Arouse1973

Adam
Dec 18, 2013
5,178
Joined
Dec 18, 2013
Messages
5,178
What sort of levels are you getting from unit going into the A/D pin?
 

Bluejets

Oct 5, 2014
6,993
Joined
Oct 5, 2014
Messages
6,993
Just wondering if you require 10k pull-ups on your I2C SDA and SCL lines as most are active low. Don't see any on your board.
 
Top