Arduino and HALL sensors problem

Hi!

I'm doing this gear indicator for my motorcyle, but I want to do it using the Arduino.

This is my code:

int topGear=6;

int upSensor=3;
int downSensor=4;
int nSensor=2;

//These are outputs to 4026
int clk=1;
int reset=0;

//This is the current gear
int gear=0;

void setup()
{
  pinMode(upSensor, INPUT);
  pinMode(downSensor, INPUT);  
  pinMode(nSensor, INPUT);
  
  pinMode(clk, OUTPUT);
  pinMode(reset, OUTPUT);
  
}
int GearUP=0;
int GearDOWN=0;

void loop()
{
  delay(50);
  
  GearUP=digitalRead(upSensor);
  GearDOWN=digitalRead(downSensor);
  
  if(GearUP==LOW)
  {
    if(gear < topGear)
    {
      gear++;      
    }
    else
    {
      gear=0;      
    }
    delay(250);    
  }
  delay(50);
  if(GearDOWN=LOW)
  {
    if(gear>0)
    {
      gear--;      
    }    
    
  }
 ShowDigit(gear);
  
}
void ShowDigit(int digit)
{
  if(gear < topGear)
  {
    digitalWrite(reset,HIGH);
    
      for(int i=0;i<digit;i++)
      {
        digitalWrite(clk, HIGH);
        delay(100);
        digitalWrite(clk,LOW);
        delay(100);    
    }
  }
  else
  {
    digitalWrite(reset,HIGH);
  } 
  
}

I think that is problem in my reading of Hall effect sensor state, I'm using Attiny85 and A3144 Hall sensors.

I tried basic example of blinking led every second, and everything work's fine, so it isn't problem in micro controller.

Thanks!

As you have not said what problems you are having with that code is is hard to say what is wrong with it.

Simply, something doesn't work, and I can't realize where is problem. Is my logic of getting state of the hall sensors well written?

Write a sketch that does nothing but read from one of the sensors and print the value it gets. Does it display the correct values? If so, do the same for the other sensor. Don't bother worrying about what else might be happening in the sketch until you know that it's reading the sensors correctly.

Problem is that I have Tiny85, on my breadboard and I'm programming it using USBAsp, and I don't know can I establish serial communication between computer and arduino.

But when I connect this hall sensor to 1 pin to 5V, 2 pin to GND, and 3 pin to one probe of my multimeter, and other probe I connect to GND. On my meter it shows about 3 V, and when I put the magnet it shows about 1mV.

It would help if you post all the code that you are using. That code has undefined variables in it and will not compile. I would not expect it to work anyway because a loop executies quickly so I would expect it to zip up or down almost immediately to the limit.

I edited code, and it compiles now, edited code is in the first post.

So what have you done about the problem I told you about in reply #5? That will stop it from working.

But another thing:-
Looking at the code you seem to put the reset high on the counter, then you pulse it, which does nothing because the reset is high, then you leave it high. Unless you put the reset low it will not count. So:-
Reset high
Reset low
pulse the counter.

on shift up It appears to give a alternating 1/0
and if you force an initial gear number, it does count down on shift down

You also need to de-bounce the gear lever (wait for it to drop back to central position)

Typical error ....

if(GearDOWN=LOW)

You need another "="

Seems to work now

Why does 6 roll to 0? does your gearbox do that?

Hi all, first of all thanks on help.

I studied this code and realized that it's full of bugs, so I decided today to write the complete new code. And I write the method's to show the digit and reset the counter and they works. And only problem remains how to fetch data from the hall sensors.
So, I have 2 hall sensor's, one which register gear up, and one which register gear down.

Let's call them UP and DOWN.

This is the code from my void loop():

int UP=0;
int DOWN=1;

int UPstate=HIGH;
int DOWNstate=HIGH;
void loop() {
  
  UPstate=digitalRead(UP);
  if(UPstate==LOW)
  {
    increment();
  }
  DOWNstate=digitalRead(DOWN);
  if(DOWNstate==LOW)
  {
     decrement();
  }
}

EDIT:
Should I use the resistor between hall sensor output and input pins?

Hi!

I put the 10k resistor, and thing works perfectly!

Thanks anyway!

LOCK!

Interesting,
I am just busy with the Attiny85 and the gear indicator. Can you share your code with me.
I am currently using the example I got from the Suzuki Vstrom, but I am more familiar with the arduino C++ code than AVR C++

Thanks in advance.

Hi adfrancois!

You mentioned that you have v strom, I think that you have the wires which are coming out of gearbox, which can be used for gear sensor so try to investigate that a little more because that principle would be better, because you don't have to use hall sensors, and it's mechanical so it's 1000% bullet proof!

This is my code, comments and some variable names are in Croatian so feel free to ask if something isn't clear: :slight_smile:

  #include <EEPROM.h>
  
  const int brojac = 1; //clk pin 4026
  const int r=0;  //reset pin  4026
  const int n=2;
  const int gornji=3; //UP sensor
  const int donji=4;  //Down sensor
  
  int trenutnaBrzina=0; //Current gear
  
  //Adress to store in eprom current gear
  const int adresa=0;
  byte spremljenaBrzina;
  
  void Spremi(byte b)
  {
    EEPROM.write(adresa,b);  
  }
  
//Method to show number using 4026 IC
  void prikaziBroj(int broj)
  {
    if(broj>trenutnaBrzina)
    {
       for(int i=0;i<broj-trenutnaBrzina;i++)
       {
         digitalWrite(brojac,HIGH);
         digitalWrite(brojac,LOW);   
       }
       
       trenutnaBrzina=broj; 
    }
    else
    {
       reset(r);
       for(int i=0;i<broj;i++)
       {
         digitalWrite(brojac,HIGH);
         digitalWrite(brojac,LOW);
       } 
       trenutnaBrzina=broj;
    }
  }
  
//This method is called when UP sensor is activated
  void visa()
  {
    if(trenutnaBrzina==1)
    {
       prikaziBroj(0);
      
    }
    else if(trenutnaBrzina==0)
    {
      prikaziBroj(2);
    }
    else if(trenutnaBrzina+1 <7)
    {
      prikaziBroj(trenutnaBrzina+1);
    }
    
    Spremi(trenutnaBrzina);
    
  }

//This method is called when DOWN sensor is activated
  void niza()
  {
    if(trenutnaBrzina==0)
    {
      prikaziBroj(1);
    }
    else if(trenutnaBrzina==2)
    {
      prikaziBroj(0);
    }
    else if(trenutnaBrzina>2 && trenutnaBrzina!=1)
    {
      prikaziBroj(trenutnaBrzina-1);
    }
    Spremi(trenutnaBrzina);
    
  }
  
  //Reset 4026
  void reset(int pin)
  {
    digitalWrite(pin,HIGH);  
    digitalWrite(pin,LOW);
  }
  
  void setup() 
  {
    
   pinMode(r,OUTPUT);
   digitalWrite(r,LOW); 
   delay(1000);
   
   pinMode(brojac, OUTPUT);    
   pinMode(gornji,INPUT);
   pinMode(donji,INPUT);
   pinMode(n,INPUT);
   digitalWrite(n,HIGH);
   digitalWrite(gornji,HIGH);
   digitalWrite(donji,HIGH);
  
   reset(r);
  
   spremljenaBrzina=EEPROM.read(adresa);
   prikaziBroj(spremljenaBrzina); 
  
  }
  //UP, DOWN, N values
  int gornjiVrij=HIGH;
  int donjiVrij=HIGH;
  int nVrij;
  
  void loop()
  {   
    
    delay(100);
    nVrij=digitalRead(n);
    if(nVrij==LOW)
    {
      prikaziBroj(0);
      delay(100);
      Spremi(0);
    }
    else
    {     
      gornjiVrij=digitalRead(gornji);
      if(gornjiVrij==LOW)
      {
        visa();
        delay(100);
      }
      delay(100);
      donjiVrij=digitalRead(donji);
      if(donjiVrij==LOW)
      {
        niza();
        delay(100);
      }
    }  
  }

Thanks a lot.
No I don't have a Vstrom, but I found some code referencing to this bike.

Your code is running, but seems I have a problem with the hall sensors, they don't pickup the signal. (I discovered I use linear, should go for digital) It only counts down when I bring PB4 to ground.
Also port PB3 seems to be defect, while it doesn't work when I bring this to ground.
I will try this weekend with another chip.

Have a nice weekend.