[Q] Proximity Sensor Calibration

Search This thread

Crossoverdoc

Senior Member
Jun 9, 2011
194
35
On Cm11.. Entering that code into the dialer doesn't work for me.

Sent from my SCH-I545 using XDA Premium 4 mobile app
 

rohanverma

Member
Jan 15, 2013
6
0
Please help, have tried this and everything else

OK. This is so weird I have tried everything.
When I check the cat state, I see the difference with finger on (210) and finger off (2). So I can conclude no hardware fault. I get my offset and push it to EFS. When I check the values the offset remains. But my proximity sensor still doesn't work.

I have tried different ROM's with clean installs (wipe cache, delvick cache, format system, etc.). I have tried the factory default samsung s3 and CM10.1.3. Which means different kernals and what not too.

I have tried compressed air and physcially taking the phone apart to see if its dirty. Nothing.

When I use something like Sensor Box, it doesn't recognize any change. Which means, from my perspective....the values from the proximity sensor aren't reaching the right place or aren't being read by the OS somewhere.

Any ideas? Can anyone help? Thanks very much in advance!

Details:
I'm on CM 10.1.3 right now (4.2.2).
i747mvluemk5

I described the sensor recalibration on Samsung rom or Samsung based roms. " *#0*# "is a "secret code" that starts a samsung test tool - only included in stock or stock rom based.

I have read from other users that "cat state" returns zero. I don't know exactly why. But it could be because:
1. that the sensor is powered down (on my phone the sensor never sleeps)
2. the calibration offset is too high
3. the calibration offset is optimal
To test the sensor is on and working you should read the value with finger put on. (there should be value around 127-calibration offset). I guess to do the calibration/sensor reading with display on and phone unlocked. Another user told me that it helped to start an app which reads the sensor values (so the sensor wake up).

But in your case proximity_sensor with the values 18,9 means that the sensor is already calibrated to 18 which can cause a zero at "cat state".
Note that the offset is read from file calibration file at boot time and from that on holded in ram. The RAM value can only be influenced by "/sys/class/sensors/proximity_sensor/prox_cal" or by calibration file (on boot). The auto calibration process not only try to create a /efs/prox_cal file but also changes the calibration value in ram at run time .
So if you try "echo 0 > /sys/class/sensors/proximity_sensor/prox_cal; echo 1 > /sys/class/sensors/proximity_sensor/prox_cal" -> "mmh auto calibration does not work there is no prox_cal file, i will try it manually" can be the wrong way because the calibration offset can be already changed to a calibrated value and so you wont get the correct blank value read out "state".


The other path in virtual filesystem to the sensor control you mentioned is also correct. I don't know which is the most common one.
But since both pathes are available in samsung stock i guess it should be " /sys/devices/virtual/sensors/proximity_sensor".



Be careful of putting in the calibration value manually. In Samsung stock rom the kernel which is reading the calibration value at boot time don't make a character conversion. So if you write the config file like this:
Code:
echo '18,9' > /efs/prox_cal
Ends Up with Calibration value of 49 because the kernel will read the first byte of file. This is a "1" which is the ASCII code for "0x31" which is 49 in decimal. That should work too but throw away half of the sensor sensivity.


If you would like to calibrate it manually you have to:

Code:
cd /sys/devices/virtual/sensors/proximity_sensor/

Read the blank value ( glass have to be clean and fingers away from sensor ).
Code:
$ cat state #finger on the sensor to test if the sensor is working properly
120
$ cat state #blank value
10

Read the actual configuration:
Code:
$ cat prox_cal
0,9

The first value is the "sensor offset" and the second one is "Cancelation Threshold" (threshold can not stored in calibration file)

So you calculate your calibration value: 0 (actual sensor offset) + 10 (blank value) = 10 (new offset) = 0x0a HEX. Make shure your efs partition is mounted writeable and then:

Code:
$ su
$ echo -en $'\x[B]0a[/B]' > /efs/prox_cal
$ sync

If the prox_cal is created first you should change the permisssions. Kernel sets 666 by default :

Code:
$ chown system:system /efs/prox_cal
$ chmod 644 /efs/prox_cal
$ sync

After reboot you can confirm the calibration

Code:
$ cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal
10,9
$ cat  /sys/devices/virtual/sensors/proximity_sensor/state
0


If you have to calibrate to a lower sensor value and want to do it the "manual way" you have to reset the calibration or delete the calibration file and reboot. because state value = sensor value - offset . And if offset gets to big the state is alway zero.
 

Formhault

Senior Member
Jun 10, 2011
11,517
4,581
Bucharest
OK. This is so weird I have tried everything.
When I check the cat state, I see the difference with finger on (210) and finger off (2). So I can conclude no hardware fault. I get my offset and push it to EFS. When I check the values the offset remains. But my proximity sensor still doesn't work.

I have tried different ROM's with clean installs (wipe cache, delvick cache, format system, etc.). I have tried the factory default samsung s3 and CM10.1.3. Which means different kernals and what not too.

I have tried compressed air and physcially taking the phone apart to see if its dirty. Nothing.

When I use something like Sensor Box, it doesn't recognize any change. Which means, from my perspective....the values from the proximity sensor aren't reaching the right place or aren't being read by the OS somewhere.

Any ideas? Can anyone help? Thanks very much in advance!

Details:
I'm on CM 10.1.3 right now (4.2.2).
i747mvluemk5

With all that you reported being taken into account... There could be a possibility that the sensor is malfunctioning :/ .
 

rohanverma

Member
Jan 15, 2013
6
0
Yeah, but it just bugs me because the sensor works like it's supposed to when I check it's cal state. The sensor is working... the information is just not getting through to where it needs to go.


With all that you reported being taken into account... There could be a possibility that the sensor is malfunctioning :/ .


---------- Post added at 10:21 PM ---------- Previous post was at 10:13 PM ----------

Update:

not a single sensor is working on the phone. I have another i747m to compare against.
I took the broken back to stock firmware. I go into the *#0*# menu and none of the sensors are working. The light, proximity, gyro, magnetic, are all off not moving numbers. Where as the functioning one has all sorts of numbers that are changing. The only number that changes is the proximity sensor..the adc value changes but the state stays to 0!

What could cause ALL sensors to not function?

What coul

Yeah, but it just bugs me because the sensor works like it's supposed to when I check it's cal state. The sensor is working... the information is just not getting through to where it needs to go.
 

mikeuz

Senior Member
Jul 30, 2012
84
8
Colima
Yeah, but it just bugs me because the sensor works like it's supposed to when I check it's cal state. The sensor is working... the information is just not getting through to where it needs to go.




---------- Post added at 10:21 PM ---------- Previous post was at 10:13 PM ----------

Update:

not a single sensor is working on the phone. I have another i747m to compare against.
I took the broken back to stock firmware. I go into the *#0*# menu and none of the sensors are working. The light, proximity, gyro, magnetic, are all off not moving numbers. Where as the functioning one has all sorts of numbers that are changing. The only number that changes is the proximity sensor..the adc value changes but the state stays to 0!

What could cause ALL sensors to not function?

What could cause ALL sensors to not function?

I may have an idea about the malfuncioning of the sensors as something like that happened to me once I restored a backup (4.1.2) and I was on 4.3 JB (apparently downgrading is not useful if using a nandroid backup)

so after a few days in doing some research and asking here on XDA nothing worked

but I had an old EFS backup (the very first one I made when I bought the phone) and decided to flash an official carrier ROM from my carrier Telcel MX and then I restored my EFS (cuz I lost my IMEI) and the problems with my sensor are gone now.

Hope it helps
 

rohanverma

Member
Jan 15, 2013
6
0
Yes, that's very helpful. I also think it has to do with the efs. I think for Samsung phones the efs is pretty important. But unfortunately I don't have an efs backup.

I understand I can't just take another phones efs folder, but could I take and restore part of it? Like where the hardware or sensor information is?

What could cause ALL sensors to not function?

I may have an idea about the malfuncioning of the sensors as something like that happened to me once I restored a backup (4.1.2) and I was on 4.3 JB (apparently downgrading is not useful if using a nandroid backup)

so after a few days in doing some research and asking here on XDA nothing worked

but I had an old EFS backup (the very first one I made when I bought the phone) and decided to flash an official carrier ROM from my carrier Telcel MX and then I restored my EFS (cuz I lost my IMEI) and the problems with my sensor are gone now.

Hope it helps
 

Formhault

Senior Member
Jun 10, 2011
11,517
4,581
Bucharest
Yeah, but it just bugs me because the sensor works like it's supposed to when I check it's cal state. The sensor is working... the information is just not getting through to where it needs to go.

Actually, your proximity sensor is not really correctly working. It should show 255 (when covered) and 20-50 when uncovered. Yours shows a value very close to 0 when uncovered and 202 when covered. :confused:

not a single sensor is working on the phone. I have another i747m to compare against.
I took the broken back to stock firmware. I go into the *#0*# menu and none of the sensors are working. The light, proximity, gyro, magnetic, are all off not moving numbers. Where as the functioning one has all sorts of numbers that are changing. The only number that changes is the proximity sensor..the adc value changes but the state stays to 0!

What could cause ALL sensors to not function?

Now that's... funky... :eek:

---------- Post added at 09:28 AM ---------- Previous post was at 09:26 AM ----------

Yes, that's very helpful. I also think it has to do with the efs. I think for Samsung phones the efs is pretty important. But unfortunately I don't have an efs backup.

I understand I can't just take another phones efs folder, but could I take and restore part of it? Like where the hardware or sensor information is?

I dont know.

you could try altought I don´t know how.

I think you can take a good file explorer, such as File Explorer (FX, with the respective Root Add-On) or ES File Explorer and manually replace all the sensors-related files in the EFS partition from another backup.

EDIT: I just checked... the EFS partition doesn't seem to contain any sensors-related files. Besides, any file I try to open is either empty, either has one character or something like that.

The (only) file inside the imei folder has a single code in it that says "BTU". LMAO.

If it would have been that simple to meddle with EFS contents, I think people wouldn't be f***ed without having a backup of it anymore. So... I don't think there's anything you can do without a backup of it :eek:.
 
Last edited:

berfles

Senior Member
Sep 4, 2011
470
55
Mine has been troublesome for months now. When I put my finger over the sensor I get 255(255,255,255) and when I remove it I get 51(51,51,51). Blowing into the earpiece seems to help sometimes, but usually it doesn't.

I tried the automatic recalibration but it didn't do anything, can anyone advise? It's a bit late right now and I can't really figure out what I should do at this point.
 

Formhault

Senior Member
Jun 10, 2011
11,517
4,581
Bucharest
Mine has been troublesome for months now. When I put my finger over the sensor I get 255(255,255,255) and when I remove it I get 51(51,51,51). Blowing into the earpiece seems to help sometimes, but usually it doesn't.

I tried the automatic recalibration but it didn't do anything, can anyone advise? It's a bit late right now and I can't really figure out what I should do at this point.

You can manually calibrate it and then make the values stick (on the file from the EFS partition). That totally fixed the problem I had on my S2 :good:.

How to calibrate.
Before you start making changes to the file from EFS, make sure the EFS partition is writable. How to mount it RW? See here. I know I failed like 3 times at this because I didn't know I had to mount EFS as RW, so the values obviously didn't stick...

That will surely help you. You just have to get it right :D.
 

berfles

Senior Member
Sep 4, 2011
470
55
You can manually calibrate it and then make the values stick (on the file from the EFS partition). That totally fixed the problem I had on my S2 :good:.

How to calibrate.
Before you start making changes to the file from EFS, make sure the EFS partition is writable. How to mount it RW? See here. I know I failed like 3 times at this because I didn't know I had to mount EFS as RW, so the values obviously didn't stick...

That will surely help you. You just have to get it right :D.

Thanks. I'm already stuck on the

Code:
$ cat state #finger on the sensor to test if the sensor is working properly
120
$ cat state #blank value
10

part though, says it's not found. I think I need to go to sleep because I'm probably missing something very simple.
 

Formhault

Senior Member
Jun 10, 2011
11,517
4,581
Bucharest
Thanks. I'm already stuck on the

Code:
$ cat state #finger on the sensor to test if the sensor is working properly
120
$ cat state #blank value
10

part though, says it's not found. I think I need to go to sleep because I'm probably missing something very simple.

My bad. This post is more comprehensive.

Still, it is not mentioned how to mount EFS as read-write there neither, so you'll still have to check the link I mentioned in my previous post.

Hope you get it right this time :D.
 
  • Like
Reactions: MEAKU15 and berfles

berfles

Senior Member
Sep 4, 2011
470
55
Thanks again. What I did so far was basically everything but that step I had issues with. I used the numbers I saw from the *#0*# menu, so since my blank value seemed to settle at 51 I used that. My offset came back as 0, so that means the offset+blank = 51 which = 33 in hex. I used that number but didn't reboot because I just wanted to test quickly and my sensor is already working as it should.

When I wake up I'll make sure EFS is read/write and try those other steps to double check my numbers and do it again.
 
  • Like
Reactions: Formhault

planetf1

Senior Member
Jan 25, 2010
825
92
Manual method worked for me.
ENJOY.

And me.

BRILLIANT! the echo'ing of 0, 1 didn't help, but the manual insertion into efs worked a treat.

Now my guess is my sensor has drifted a lot - I ended up with a value of 7B, so it's perhaps a sign of pending failure - not sure what others get, but it's fixed the immediate issue. even though my phone is under warranty (though knox=1) it would be hassle resetting and being without for a while!
 
  • Like
Reactions: Formhault

lettereleven

Member
Nov 2, 2013
17
2
Vancouver
Manual calibration is actually easy and works.

Call me lame, but I feared Terminal for many months. I looked for multiple ways of fixing my sensor issue, even considered reverting to stock S4 (yuck). Big thanks to those that made this "how to fix your sensor". The manual method below totally works, and its really not scary.

Note for the noobs (like me): make sure you enter "su" to get super user/root access. You dont actually type the hash marks. You get those when you are in super user mode. No super user, no work. I know, totally obvious for clever people, but i didnt know.

-Jamie.
Galaxy S4 running Danvh's incredible Dandroid rom


Methods that worked for me.

Method : finding two values for calibration . offset + normal value.
##.. FINDING OFFSET ######
cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal

note down the values it returns.. mine is 5,64,100.. you only need the 1st value which is 5.
### FINDING NORMAL STATE ######

cat /sys/devices/virtual/sensors/proximity_sensor/state
note down the value if returns. mine is 64

now ADD OFFSET value + Normal Value
5 + 64 = 69
Convert in Hexadecimal which is 45.

<<<<<<<<<<< Pushing the value in /efs/prox_cal >>>>>>>>>>>>>>

# echo -en $ '\x45' > /efs/prox_cal

then
# chown system:system /efs/prox_cal
# chmod 644 /efs/prox_cal
# sync
# Reboot

All done. after that go to *#0*# then SENSORS and move your finger on the sensor, yout phone will Vibrate only when you put your finger on the sensors.

Manual method worked for me.
ENJOY.
 
  • Like
Reactions: Formhault

AMoosa

Senior Member
Sep 24, 2006
2,119
349
Samsung Galaxy Z Fold2
I was a bit wary of the manual method. I tried it with the values reported an by my phone and it didn't work. Then I tried it with the values in the example and I totally worked. Awesome! I've been struggling for months manually turning my screen on and off (first world problem), but all fixed now. :)

Sent from my GT-I9505G using Tapatalk
 
  • Like
Reactions: Formhault

Top Liked Posts

  • There are no posts matching your filters.
  • 20
    ... it seems to go back to being broken when I reboot the phone. Is there any way to prevent this? Also the *#0*# doesn't work for me and the cat command returns just a 0. Thanks.

    EDIT: it might help to note that I'm running cyanogenmod 10.1 nightly now
    EDIT2: I figured it out! I did a search for prox_cal and found one in /sys/devices/virtual/sensors/proximity_sensor with the values 18,9. i put those values in the empty /efs/prox_cal. It now works on boot. :)

    I described the sensor recalibration on Samsung rom or Samsung based roms. " *#0*# "is a "secret code" that starts a samsung test tool - only included in stock or stock rom based.

    I have read from other users that "cat state" returns zero. I don't know exactly why. But it could be because:
    1. that the sensor is powered down (on my phone the sensor never sleeps)
    2. the calibration offset is too high
    3. the calibration offset is optimal
    To test the sensor is on and working you should read the value with finger put on. (there should be value around 127-calibration offset). I guess to do the calibration/sensor reading with display on and phone unlocked. Another user told me that it helped to start an app which reads the sensor values (so the sensor wake up).

    But in your case proximity_sensor with the values 18,9 means that the sensor is already calibrated to 18 which can cause a zero at "cat state".
    Note that the offset is read from file calibration file at boot time and from that on holded in ram. The RAM value can only be influenced by "/sys/class/sensors/proximity_sensor/prox_cal" or by calibration file (on boot). The auto calibration process not only try to create a /efs/prox_cal file but also changes the calibration value in ram at run time .
    So if you try "echo 0 > /sys/class/sensors/proximity_sensor/prox_cal; echo 1 > /sys/class/sensors/proximity_sensor/prox_cal" -> "mmh auto calibration does not work there is no prox_cal file, i will try it manually" can be the wrong way because the calibration offset can be already changed to a calibrated value and so you wont get the correct blank value read out "state".


    The other path in virtual filesystem to the sensor control you mentioned is also correct. I don't know which is the most common one.
    But since both pathes are available in samsung stock i guess it should be " /sys/devices/virtual/sensors/proximity_sensor".



    Be careful of putting in the calibration value manually. In Samsung stock rom the kernel which is reading the calibration value at boot time don't make a character conversion. So if you write the config file like this:
    Code:
    echo '18,9' > /efs/prox_cal
    Ends Up with Calibration value of 49 because the kernel will read the first byte of file. This is a "1" which is the ASCII code for "0x31" which is 49 in decimal. That should work too but throw away half of the sensor sensivity.


    If you would like to calibrate it manually you have to:

    Code:
    cd /sys/devices/virtual/sensors/proximity_sensor/

    Read the blank value ( glass have to be clean and fingers away from sensor ).
    Code:
    $ cat state #finger on the sensor to test if the sensor is working properly
    120
    $ cat state #blank value
    10

    Read the actual configuration:
    Code:
    $ cat prox_cal
    0,9

    The first value is the "sensor offset" and the second one is "Cancelation Threshold" (threshold can not stored in calibration file)

    So you calculate your calibration value: 0 (actual sensor offset) + 10 (blank value) = 10 (new offset) = 0x0a HEX. Make shure your efs partition is mounted writeable and then:

    Code:
    $ su
    $ echo -en $'\x[B]0a[/B]' > /efs/prox_cal
    $ sync

    If the prox_cal is created first you should change the permisssions. Kernel sets 666 by default :

    Code:
    $ chown system:system /efs/prox_cal
    $ chmod 644 /efs/prox_cal
    $ sync

    After reboot you can confirm the calibration

    Code:
    $ cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal
    10,9
    $ cat  /sys/devices/virtual/sensors/proximity_sensor/state
    0


    If you have to calibrate to a lower sensor value and want to do it the "manual way" you have to reset the calibration or delete the calibration file and reboot. because state value = sensor value - offset . And if offset gets to big the state is alway zero.
    15
    On Samsung devices like i9100 (Galaxy S3) or N7100 (Note II) you can do the following if you have root permissions:

    On Android Terminal App or adb shell do:
    Code:
    $ su
    $ echo 0 > /sys/class/sensors/proximity_sensor/prox_cal
    $ echo 1 > /sys/class/sensors/proximity_sensor/prox_cal

    The zero resets the actual calibration offset.
    The one does an auto calibration (read sensor value, set offset to this sensor value, write calibration offset to "/efs/prox_cal" which is read while device is booting).
    Best for calibrate your sensor is to keep it dark, the glass have to be clean and the sensor free (so that the sensor doesn't measure anything).
    The calibration offset is stored in efs, thats why you can make full wipe and use other ROMs without the need of recalibration.

    You can check the running config by:
    Code:
    $ cat /sys/class/sensors/proximity_sensor/state
    The first value is the calibration Offset, the second one is the sensor threshold (threshold is set at compile time).

    Open *#0*# to see "sensors" and check if your proximity sensor is now working properly.


    Because in Kernel code (copyright by Samsung) for the device driver is somthing like this autocalibration, i bet that there is a secret menue or code that does the calibration too. But until someone discovers this secret we have to have root permissions to do a proximity sensor calibration.
    12
    FIXED. Thanks to mcelicalderon, Finally Fixed

    Methods that worked for me.
    Method 1.
    Blow Air it will solve your prob hopefully.

    Method 2.
    go to samsung code *#0*#. then SENSORS and Read your Proximity sensor values.

    <<< DECISION >>>>>
    ####Calibration isnot needed if ####
    .
    your phone doesnt vibrate in normal but does so when u physically put your finger on the Sensor.

    #### Calibration needed if ####
    .
    your phone is vibrating in this menu no matter what, you put your finger on the sensor or not
    Follow these steps
    1. download Android Terminal from store
    2. type these commands to calibrate automatically
    $ su
    # echo 0 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal
    # echo 1 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal

    Reboot and see if it works.go to *#0*# and check if it continuously vibrating or only does so when you put your finger on the SENSOR.

    <<<< IF Continuously Vibrating Follow these steps>>>>

    you need to manually put values in ur /efs/prox_cal.it Reads Hexadecimal value. so you have to convert your decimal values into HEX.

    ##### FIND YOUR Decimal Values and pushing them into /efs/prox_cal ######

    Method : finding two values for calibration . offset + normal value.
    ##.. FINDING OFFSET ######
    cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal

    note down the values it returns.. mine is 5,64,100.. you only need the 1st value which is 5.
    ### FINDING NORMAL STATE ######

    cat /sys/devices/virtual/sensors/proximity_sensor/state
    note down the value if returns. mine is 64

    now ADD OFFSET value + Normal Value
    5 + 64 = 69
    Convert in Hexadecimal which is 45.

    <<<<<<<<<<< Pushing the value in /efs/prox_cal >>>>>>>>>>>>>>

    # echo -en $ '\x45' > /efs/prox_cal

    then
    # chown system:system /efs/prox_cal
    # chmod 644 /efs/prox_cal
    # sync
    # Reboot

    All done. after that go to *#0*# then SENSORS and move your finger on the sensor, yout phone will Vibrate only when you put your finger on the sensors.

    Manual method worked for me.
    ENJOY.
    9
    Finally Fixed My Sensor

    I described the sensor recalibration on Samsung rom or Samsung based roms. " *#0*# "is a "secret code" that starts a samsung test tool - only included in stock or stock rom based.

    I have read from other users that "cat state" returns zero. I don't know exactly why. But it could be because:
    1. that the sensor is powered down (on my phone the sensor never sleeps)
    2. the calibration offset is too high
    3. the calibration offset is optimal
    To test the sensor is on and working you should read the value with finger put on. (there should be value around 127-calibration offset). I guess to do the calibration/sensor reading with display on and phone unlocked. Another user told me that it helped to start an app which reads the sensor values (so the sensor wake up).

    But in your case proximity_sensor with the values 18,9 means that the sensor is already calibrated to 18 which can cause a zero at "cat state".
    Note that the offset is read from file calibration file at boot time and from that on holded in ram. The RAM value can only be influenced by "/sys/class/sensors/proximity_sensor/prox_cal" or by calibration file (on boot). The auto calibration process not only try to create a /efs/prox_cal file but also changes the calibration value in ram at run time .
    So if you try "echo 0 > /sys/class/sensors/proximity_sensor/prox_cal; echo 1 > /sys/class/sensors/proximity_sensor/prox_cal" -> "mmh auto calibration does not work there is no prox_cal file, i will try it manually" can be the wrong way because the calibration offset can be already changed to a calibrated value and so you wont get the correct blank value read out "state".


    The other path in virtual filesystem to the sensor control you mentioned is also correct. I don't know which is the most common one.
    But since both pathes are available in samsung stock i guess it should be " /sys/devices/virtual/sensors/proximity_sensor".



    Be careful of putting in the calibration value manually. In Samsung stock rom the kernel which is reading the calibration value at boot time don't make a character conversion. So if you write the config file like this:
    Code:
    echo '18,9' > /efs/prox_cal
    Ends Up with Calibration value of 49 because the kernel will read the first byte of file. This is a "1" which is the ASCII code for "0x31" which is 49 in decimal. That should work too but throw away half of the sensor sensivity.


    If you would like to calibrate it manually you have to:

    Code:
    cd /sys/devices/virtual/sensors/proximity_sensor/

    Read the blank value ( glass have to be clean and fingers away from sensor ).
    Code:
    $ cat state #finger on the sensor to test if the sensor is working properly
    120
    $ cat state #blank value
    10

    Read the actual configuration:
    Code:
    $ cat prox_cal
    0,9

    The first value is the "sensor offset" and the second one is "Cancelation Threshold" (threshold can not stored in calibration file)

    So you calculate your calibration value: 0 (actual sensor offset) + 10 (blank value) = 10 (new offset) = 0x0a HEX. Make shure your efs partition is mounted writeable and then:

    Code:
    $ su
    $ echo -en $'\x[B]0a[/B]' > /efs/prox_cal
    $ sync

    If the prox_cal is created first you should change the permisssions. Kernel sets 666 by default :

    Code:
    $ chown system:system /efs/prox_cal
    $ chmod 644 /efs/prox_cal
    $ sync

    After reboot you can confirm the calibration

    Code:
    $ cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal
    10,9
    $ cat  /sys/devices/virtual/sensors/proximity_sensor/state
    0


    If you have to calibrate to a lower sensor value and want to do it the "manual way" you have to reset the calibration or delete the calibration file and reboot. because state value = sensor value - offset . And if offset gets to big the state is alway zero.

    Finally, this fixed my problem. I don't know why, but the other solution worked one time while using a CM 10.2 nightly

    Code:
    $ su
    $ echo 0 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal
    $ echo 1 > /sys/devices/virtual/sensors/proximity_sensor/prox_cal

    But, when I upgraded to the latest nightly, this stopped working. It wouldn't work even if y restored my stock ROM backup using TWRP, I even flashed a stock image with odin and the sensor would still malfunction.

    Today I followed this steps using the values returned for my phone and it finally worked! I did this.

    Code:
    $ cat state #finger on the sensor to test if the sensor is working properly
    120
    $ cat state #blank value
    10

    This returned 255 #finger on the sensor and 69 #blank value

    and when I did
    Code:
     cat /sys/devices/virtual/sensors/proximity_sensor/prox_cal

    this returned 47,60,45.....so my offset was 47. Ths sum of offset and #blank value was 47 + 69 = 116 that is equal to 74 in HEX.

    so I did

    Code:
    $ echo -en $'\x74' > /efs/prox_cal

    and then

    Code:
    $ chown system:system /efs/prox_cal
    $ chmod 644 /efs/prox_cal
    $ sync

    After that I rebooted my phone from the terminal using que command

    Code:
    $ reboot

    and that was it! Now my sensor is fixed and survives reboot. Thanks!
    2
    On Samsung devices like i9100 (Galaxy S3) or N7100 (Note II) you can do the following if you have root permissions:

    On Android Terminal App or adb shell do:
    Code:
    $ su
    $ echo 0 > /sys/class/sensors/proximity_sensor/prox_cal
    $ echo 1 > /sys/class/sensors/proximity_sensor/prox_cal

    The zero resets the actual calibration offset.
    The one does an auto calibration (read sensor value, set offset to this sensor value, write calibration offset to "/efs/prox_cal" which is read while device is booting).
    Best for calibrate your sensor is to keep it dark, the glass have to be clean and the sensor free (so that the sensor doesn't measure anything).
    The calibration offset is stored in efs, thats why you can make full wipe and use other ROMs without the need of recalibration.

    You can check the running config by:
    Code:
    $ cat /sys/class/sensors/proximity_sensor/state
    The first value is the calibration Offset, the second one is the sensor threshold (threshold is set at compile time).

    Open *#0*# to see "sensors" and check if your proximity sensor is now working properly.


    Because in Kernel code (copyright by Samsung) for the device driver is somthing like this autocalibration, i bet that there is a secret menue or code that does the calibration too. But until someone discovers this secret we have to have root permissions to do a proximity sensor calibration.

    Hey, I've been looking for a fix for prox sensor on my Note 2 ever since I installed Paranoid back in december/january, went back to stock and it was still broken. This fixed it!! So thank you :) I have just one issue, it seems to go back to being broken when I reboot the phone. Is there any way to prevent this? Also the *#0*# doesn't work for me and the cat command returns just a 0. Thanks.

    EDIT: it might help to note that I'm running cyanogenmod 10.1 nightly now
    EDIT2: I figured it out! I did a search for prox_cal and found one in /sys/devices/virtual/sensors/proximity_sensor with the values 18,9. i put those values in the empty /efs/prox_cal. It now works on boot. :)